mirror of
https://github.com/openziti/zrok.git
synced 2025-06-25 04:02:15 +02:00
minimum infrastructure for pastebin sdk example (#379)
This commit is contained in:
parent
1130ff8006
commit
6eddcedb32
60
sdk/examples/pastebin/cmd/copyto/main.go
Normal file
60
sdk/examples/pastebin/cmd/copyto/main.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/openziti/zrok/environment"
|
||||||
|
"github.com/openziti/zrok/sdk"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
root, err := environment.LoadRoot()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
shr, err := sdk.CreateShare(root, &sdk.ShareRequest{
|
||||||
|
BackendMode: sdk.TcpTunnelBackendMode,
|
||||||
|
ShareMode: sdk.PrivateShareMode,
|
||||||
|
Target: "pastebin",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("access your pastebin with: 'pastefrom %v'\n", shr.Token)
|
||||||
|
|
||||||
|
listener, err := sdk.NewListener(shr.Token, root)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := make(chan os.Signal)
|
||||||
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-c
|
||||||
|
if err := sdk.DeleteShare(root, shr); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_ = listener.Close()
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
if conn, err := listener.Accept(); err == nil {
|
||||||
|
go handle(conn)
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handle(conn net.Conn) {
|
||||||
|
_, err := conn.Write([]byte("hello from pastebin"))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
46
sdk/examples/pastebin/cmd/pastefrom/main.go
Normal file
46
sdk/examples/pastebin/cmd/pastefrom/main.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/openziti/zrok/environment"
|
||||||
|
"github.com/openziti/zrok/sdk"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
panic("usage: pastefrom <shrToken>")
|
||||||
|
}
|
||||||
|
shrToken := os.Args[1]
|
||||||
|
|
||||||
|
root, err := environment.LoadRoot()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
acc, err := sdk.CreateAccess(root, &sdk.AccessRequest{ShareToken: shrToken})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := sdk.DeleteAccess(root, acc); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
conn, err := sdk.NewDialer(shrToken, root)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
buf := make([]byte, 10240)
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(buf[:n]))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user