adjust for tun2 api changes

This commit is contained in:
Cadey Ratio 2017-10-04 00:00:16 -07:00
parent 59a3f45150
commit c95a8c5434
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
2 changed files with 65 additions and 8 deletions

View File

@ -35,7 +35,7 @@ func main() {
client, _ := tun2.NewClient(cfg)
for {
err := client.Connect()
err := client.Connect(context.Background())
if err != nil {
ln.Error(context.Background(), err, ln.Action("client connection failed"))
}

View File

@ -11,8 +11,11 @@ import (
"git.xeserv.us/xena/route/internal/database"
"git.xeserv.us/xena/route/internal/tun2"
proto "git.xeserv.us/xena/route/proto"
"github.com/Xe/ln"
"github.com/mtneug/pkg/ulid"
kcp "github.com/xtaci/kcp-go"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
@ -46,6 +49,59 @@ type Config struct {
CertKey *[32]byte
}
func (s *Server) listenTCP(ctx context.Context, addr string, tcfg *tls.Config) {
l, err := tls.Listen("tcp", addr, tcfg)
if err != nil {
panic(err)
}
ln.Log(ctx, ln.Action("tcp+tls listening"), ln.F{"addr": l.Addr()})
for {
conn, err := l.Accept()
if err != nil {
ln.Error(ctx, err, ln.Action("accept backend client socket"))
}
ln.Log(ctx, ln.F{
"action": "new backend client",
"addr": conn.RemoteAddr(),
"network": conn.RemoteAddr().Network(),
})
go s.ts.HandleConn(conn, false)
}
}
func (s *Server) listenKCP(ctx context.Context, addr string, tcfg *tls.Config) {
l, err := kcp.Listen(addr)
if err != nil {
panic(err)
}
ln.Log(ctx, ln.F{
"action": "kcp+tls listening",
"addr": l.Addr(),
})
for {
conn, err := l.Accept()
if err != nil {
ln.Error(ctx, err, ln.F{"kind": "kcp", "addr": l.Addr().String()})
}
ln.Log(ctx, ln.F{
"action": "new_client",
"network": conn.RemoteAddr().Network(),
"addr": conn.RemoteAddr(),
})
tc := tls.Server(conn, tcfg)
go s.ts.HandleConn(tc, true)
}
}
// New creates a new Server
func New(cfg Config) (*Server, error) {
if cfg.CertKey == nil {
@ -65,11 +121,6 @@ func New(cfg Config) (*Server, error) {
}
tcfg := &tun2.ServerConfig{
TCPAddr: cfg.BackendTCPAddr,
KCPAddr: cfg.BackendKCPAddr,
TLSConfig: &tls.Config{
GetCertificate: m.GetCertificate,
},
Storage: &storageWrapper{
Storage: db,
},
@ -79,6 +130,7 @@ func New(cfg Config) (*Server, error) {
if err != nil {
return nil, err
}
s := &Server{
cfg: &cfg,
db: db,
@ -87,8 +139,13 @@ func New(cfg Config) (*Server, error) {
Manager: m,
}
s.ts = ts
go ts.ListenAndServe()
s.listenKCP(context.Background(), cfg.BackendKCPAddr, &tls.Config{
GetCertificate: m.GetCertificate,
})
s.listenTCP(context.Background(), cfg.BackendTCPAddr, &tls.Config{
GetCertificate: m.GetCertificate,
})
gs := grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
GetCertificate: m.GetCertificate,