forked from skycoin/skywire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskysocks-client.go
101 lines (80 loc) · 2.17 KB
/
skysocks-client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
proxy client app for skywire visor
*/
package main
import (
"flag"
"io"
"net"
"time"
"github.com/sirupsen/logrus"
"github.com/skycoin/dmsg/buildinfo"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
const (
netType = appnet.TypeSkynet
socksPort = routing.Port(3)
)
var log = logrus.New()
var r = netutil.NewRetrier(time.Second, 0, 1)
func dialServer(appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
var conn net.Conn
err := r.Do(func() error {
var err error
conn, err = appCl.Dial(appnet.Addr{
Net: netType,
PubKey: pk,
Port: socksPort,
})
return err
})
if err != nil {
return nil, err
}
return conn, nil
}
func main() {
appC := app.NewClient(nil)
defer appC.Close()
skysocks.Log = log
if _, err := buildinfo.Get().WriteTo(log.Writer()); err != nil {
log.Printf("Failed to output build info: %v", err)
}
var addr = flag.String("addr", skyenv.SkysocksClientAddr, "Client address to listen on")
var serverPK = flag.String("srv", "", "PubKey of the server to connect to")
flag.Parse()
if *serverPK == "" {
log.Warn("Empty server PubKey. Exiting")
return
}
pk := cipher.PubKey{}
if err := pk.UnmarshalText([]byte(*serverPK)); err != nil {
log.Fatal("Invalid server PubKey: ", err)
}
for {
conn, err := dialServer(appC, pk)
if err != nil {
log.Fatalf("Failed to dial to a server: %v", err)
}
log.Printf("Connected to %v\n", pk)
client, err := skysocks.NewClient(conn)
if err != nil {
log.Fatal("Failed to create a new client: ", err)
}
log.Printf("Serving proxy client %v\n", *addr)
if err := client.ListenAndServe(*addr); err != nil {
log.Errorf("Error serving proxy client: %v\n", err)
}
// need to filter this out, cause usually client failure means app conn is already closed
if err := conn.Close(); err != nil && err != io.ErrClosedPipe {
log.Errorf("Error closing app conn: %v\n", err)
}
log.Println("Reconnecting to skysocks server")
}
}