From 7fa19da36e41efc4a395f5051757156c806f5ff6 Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Mon, 2 Dec 2019 02:19:57 +0000 Subject: [PATCH 1/7] --- src/make/tmp/tmpTls/main.go | 57 ++++++++++++++--------------- src/make/tmp/tmpTls/newCert.go | 63 --------------------------------- src/tachyonVpnClient/client.go | 12 +++++++ src/tachyonVpnClient/connect.go | 4 +-- src/tachyonVpnServer/server.go | 1 + 5 files changed, 42 insertions(+), 95 deletions(-) delete mode 100644 src/make/tmp/tmpTls/newCert.go diff --git a/src/make/tmp/tmpTls/main.go b/src/make/tmp/tmpTls/main.go index b4ede3a..a49d396 100644 --- a/src/make/tmp/tmpTls/main.go +++ b/src/make/tmp/tmpTls/main.go @@ -4,19 +4,19 @@ import ( "crypto/tls" "tyTls" "fmt" - "sync" "github.com/tachyon-protocol/udw/udwErr" - "github.com/tachyon-protocol/udw/udwTest" "github.com/tachyon-protocol/udw/udwNet/udwNetTestV2" - "net" + "crypto/rsa" + "crypto/x509" + "crypto/ecdsa" ) func main(){ tyTls.EnableTlsVersion13() - certS :=newCert(false) + certS :=tyTls.NewTlsCert(false) ServerChk :=tyTls.HashChk(certS.Certificate[0]) fmt.Println("ServerChk",ServerChk) - certC:=newCert(true) + certC:=tyTls.NewTlsCert(true) ClientChk :=tyTls.HashChk(certC.Certificate[0]) fmt.Println("ClientChk",ClientChk) { @@ -34,33 +34,30 @@ func main(){ c1,c2:=udwNetTestV2.MustTcpPipe() tlsC:=tls.Client(c1,cc) tlsS:=tls.Server(c2,sc) - TestTwoNetConn(tlsC,tlsS) + udwNetTestV2.RunTestTwoRwc(tlsC,tlsS) } } -func TestTwoNetConn(tlsC net.Conn,tlsS net.Conn){ - wg:=sync.WaitGroup{} - wg.Add(1) - go func(){ - buf:=make([]byte,4096) - nr,err:=tlsS.Read(buf) - fmt.Println("5") - udwErr.PanicIfError(err) - udwTest.Equal(buf[:nr],[]byte{1}) - wg.Done() - }() - _,err:=tlsC.Write([]byte{1}) - udwErr.PanicIfError(err) - fmt.Println("3") - wg.Wait() - for i:=0;i<10;i++{ - _,err:=tlsC.Write([]byte{1}) - udwErr.PanicIfError(err) - buf:=make([]byte,4096) - nr,err:=tlsS.Read(buf) - udwErr.PanicIfError(err) - udwTest.Equal(buf[:nr],[]byte{1}) +func CertMarshal(cert *tls.Certificate) []byte{ + +} + +func CertUnmarshal(b []byte) (cert *tls.Certificate){ + +} + +func marshalPrivateKey(objI interface{}) (b []byte,errMsg string){ + switch obj:= objI.(type) { + case *rsa.PrivateKey: + b := x509.MarshalPKCS1PrivateKey(obj) + return b,"" + case *ecdsa.PrivateKey: + b, err := x509.MarshalECPrivateKey(obj) + if err != nil { + return nil, err.Error() + } + return b,"" + default: + return nil,"unknow privateKey type" } - tlsC.Close() - tlsS.Close() } \ No newline at end of file diff --git a/src/make/tmp/tmpTls/newCert.go b/src/make/tmp/tmpTls/newCert.go deleted file mode 100644 index f3afdd4..0000000 --- a/src/make/tmp/tmpTls/newCert.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "time" - "crypto/x509" - "math/big" - "crypto/ecdsa" - "crypto/elliptic" - "encoding/pem" - "crypto/tls" - "crypto/rand" - "net" -) - -func newCert(isClient bool) (cert tls.Certificate){ - var ExtKeyUsage x509.ExtKeyUsage - if isClient{ - ExtKeyUsage = x509.ExtKeyUsageClientAuth - }else{ - ExtKeyUsage = x509.ExtKeyUsageServerAuth - } - const dur = 100*365*24*time.Hour - startTime:=time.Now() - notBefore:=startTime.Add(-dur) - notAfter:=startTime.Add(dur) - template := x509.Certificate{ - SerialNumber: big.NewInt(1), - NotBefore: notBefore, - NotAfter: notAfter, - KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, - ExtKeyUsage: []x509.ExtKeyUsage{ExtKeyUsage}, - BasicConstraintsValid: true, - } - if isClient==false{ - template.IPAddresses = []net.IP{net.IPv4(127,0,0,1)} - } - //template.DNSNames = []string{"google.com"} - priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err!=nil{ - panic(err) - } - derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) - if err != nil { - panic(err) - } - certPem:=pem.EncodeToMemory(&pem.Block{ - Type: "CERTIFICATE", - Bytes: derBytes, - }) - b,err := x509.MarshalECPrivateKey(priv) - if err!=nil{ - panic(err) - } - privPem:=pem.EncodeToMemory(&pem.Block{ - Type: "EC PRIVATE KEY", - Bytes: b, - }) - cert, err = tls.X509KeyPair(certPem, privPem) - if err != nil { - panic(err) - } - return cert -} \ No newline at end of file diff --git a/src/tachyonVpnClient/client.go b/src/tachyonVpnClient/client.go index d14911f..a2ef2ff 100644 --- a/src/tachyonVpnClient/client.go +++ b/src/tachyonVpnClient/client.go @@ -30,6 +30,8 @@ type RunReq struct { IsRelay bool ExitServerClientId uint64 //required when IsRelay is true ExitServerTKey string //required when IsRelay is true + + ServerChk string // if it is "", it will use InsecureSkipVerify } type Client struct { @@ -40,6 +42,7 @@ type Client struct { connLock sync.Mutex directVpnConn net.Conn vpnConn net.Conn + tlsConfig *tls.Config } func (c *Client) Run(req RunReq) { @@ -56,6 +59,15 @@ func (c *Client) Run(req RunReq) { tun, err := createTun(req.ServerIp) udwErr.PanicIfError(err) //err = c.connect() + if req.ServerChk==""{ + c.tlsConfig = newInsecureClientTlsConfig() + }else{ + var errMsg string + c.tlsConfig,errMsg = tyTls.NewClientTlsConfigWithChk(tyTls.NewClientTlsConfigWithChkReq{ + ServerChk: req.ServerChk, + }) + udwErr.PanicIfErrorMsg(errMsg) + } c.reconnect() c.keepAliveThread() go func() { diff --git a/src/tachyonVpnClient/connect.go b/src/tachyonVpnClient/connect.go index 23eaedb..5855419 100644 --- a/src/tachyonVpnClient/connect.go +++ b/src/tachyonVpnClient/connect.go @@ -19,7 +19,7 @@ func (c *Client) connect() error { if err != nil { return errors.New("[w7syh9d1zgd] " + err.Error()) } - vpnConn = tls.Client(vpnConn, newInsecureClientTlsConfig()) + vpnConn = tls.Client(vpnConn, c.tlsConfig) var ( handshakeVpnPacket = tachyonVpnProtocol.VpnPacket{ Cmd: tachyonVpnProtocol.CmdHandshake, @@ -44,7 +44,7 @@ func (c *Client) connect() error { connRelaySide, plain = tachyonVpnProtocol.NewInternalConnectionDual(nil, nil) relayConn = vpnConn ) - vpnConn = tls.Client(plain, newInsecureClientTlsConfig()) + vpnConn = tls.Client(plain, c.tlsConfig) //read from relay conn, write to vpn conn go func() { var ( diff --git a/src/tachyonVpnServer/server.go b/src/tachyonVpnServer/server.go index e5f486e..57ac108 100644 --- a/src/tachyonVpnServer/server.go +++ b/src/tachyonVpnServer/server.go @@ -26,6 +26,7 @@ type ServerRunReq struct { UseRelay bool RelayServerIp string RelayServerTKey string + RelayServerChk string SelfTKey string } From 0b11465ed16ba48042ecc1ffd5ed43e12aa0a780 Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Fri, 6 Dec 2019 09:01:50 +0000 Subject: [PATCH 2/7] --- src/make/tmp/tmpTls/main.go | 116 ++++++++++++++++++++++++++------- src/make/tmp/tmpTls/newCert.go | 61 +++++++++++++++++ 2 files changed, 155 insertions(+), 22 deletions(-) create mode 100644 src/make/tmp/tmpTls/newCert.go diff --git a/src/make/tmp/tmpTls/main.go b/src/make/tmp/tmpTls/main.go index a49d396..f51712c 100644 --- a/src/make/tmp/tmpTls/main.go +++ b/src/make/tmp/tmpTls/main.go @@ -6,16 +6,40 @@ import ( "fmt" "github.com/tachyon-protocol/udw/udwErr" "github.com/tachyon-protocol/udw/udwNet/udwNetTestV2" - "crypto/rsa" "crypto/x509" - "crypto/ecdsa" + "github.com/tachyon-protocol/udw/udwBytes" + "github.com/tachyon-protocol/udw/udwCryptoSha3" + "encoding/base64" + "encoding/pem" + "bytes" ) +type ClientToken struct{ + ServerChk string + ClientCert string +} + func main(){ tyTls.EnableTlsVersion13() + { + cert_1:=NewTlsCert(false,"27cz7epj6m") + s_1,errMsg:=CertMarshal(&cert_1) + udwErr.PanicIfErrorMsg(errMsg) + cert_2:=NewTlsCert(false,"27cz7epj6m") + s_2,errMsg:=CertMarshal(&cert_2) + udwErr.PanicIfErrorMsg(errMsg) + fmt.Println(s_1==s_2) + } certS :=tyTls.NewTlsCert(false) ServerChk :=tyTls.HashChk(certS.Certificate[0]) - fmt.Println("ServerChk",ServerChk) + s,errMsg:=CertMarshal(&certS) + udwErr.PanicIfErrorMsg(errMsg) + fmt.Println("ServerCert",s,len(s)) + certS_1,errMsg:=CertUnmarshal(s) + udwErr.PanicIfErrorMsg(errMsg) + certS=*certS_1 + + fmt.Println("ServerChk",ServerChk,len(ServerChk)) certC:=tyTls.NewTlsCert(true) ClientChk :=tyTls.HashChk(certC.Certificate[0]) fmt.Println("ClientChk",ClientChk) @@ -38,26 +62,74 @@ func main(){ } } -func CertMarshal(cert *tls.Certificate) []byte{ - -} - -func CertUnmarshal(b []byte) (cert *tls.Certificate){ - +func CertMarshal(cert *tls.Certificate) (s string,errMsg string){ + buf:=udwBytes.BufWriter{} + pkB,err:=x509.MarshalPKCS8PrivateKey(cert.PrivateKey) + if err!=nil{ + return "",err.Error() + } + buf.WriteUvarint(uint64(len(pkB))) + buf.Write_(pkB) + buf.WriteUvarint(uint64(len(cert.Certificate))) + for _, certRaw:=range cert.Certificate{ + buf.WriteUvarint(uint64(len(certRaw))) + buf.Write_(certRaw) + } + sum:=udwCryptoSha3.Sum512Slice(buf.GetBytes()) + buf.Write_(sum[:4]) + return base64.RawURLEncoding.EncodeToString(buf.GetBytes()),"" } -func marshalPrivateKey(objI interface{}) (b []byte,errMsg string){ - switch obj:= objI.(type) { - case *rsa.PrivateKey: - b := x509.MarshalPKCS1PrivateKey(obj) - return b,"" - case *ecdsa.PrivateKey: - b, err := x509.MarshalECPrivateKey(obj) - if err != nil { - return nil, err.Error() +func CertUnmarshal(s string) (tlsCert *tls.Certificate,errMsg string){ + b,err:=base64.RawURLEncoding.DecodeString(s) + if err!=nil{ + return nil,err.Error() + } + reader:=udwBytes.NewBufReader(b) + pkSize,ok:=reader.ReadUvarint() + if !ok|| pkSize<=1{ + return nil,"gmfp28u374" + } + pkB,ok:=reader.ReadByteSlice(int(pkSize)) + if !ok{ + return nil,"nkt4xh9mfe" + } + pkPem:=pem.EncodeToMemory(&pem.Block{ + Type: "PRIVATE KEY", + Bytes: pkB, + }) + certListLen,ok:=reader.ReadUvarint() + if !ok{ + return nil,"yetwm28kyj" + } + certBuf:=udwBytes.BufWriter{} + for i:=0;i Date: Fri, 6 Dec 2019 15:02:37 +0000 Subject: [PATCH 3/7] --- src/github.com/tachyon-protocol/udw | 2 +- src/make/tmp/main.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/make/tmp/main.go diff --git a/src/github.com/tachyon-protocol/udw b/src/github.com/tachyon-protocol/udw index f45e5da..c7ec65b 160000 --- a/src/github.com/tachyon-protocol/udw +++ b/src/github.com/tachyon-protocol/udw @@ -1 +1 @@ -Subproject commit f45e5daa2d82b5a813d80019993ba346ef049ba5 +Subproject commit c7ec65b78eb6e4ab27030548508a8b26d429242e diff --git a/src/make/tmp/main.go b/src/make/tmp/main.go new file mode 100644 index 0000000..073214d --- /dev/null +++ b/src/make/tmp/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/tachyon-protocol/udw/udwRpc2/udwRpc2Tester" + +func main(){ + udwRpc2Tester.BuildAndTest() +} From a737de7cb6b12dede0875464b848701a99c9753a Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Sat, 7 Dec 2019 03:12:54 +0000 Subject: [PATCH 4/7] --- README.md | 4 +++ src/github.com/tachyon-protocol/udw | 2 +- src/tachyonVpnServer/server.go | 44 +++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d1d72c7..bd7373c 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,7 @@ - run relay server `server -SelfTKey [relay server's TKey]` - run exit server `server -SelfTKey [exit server's TKey] -UseRelay -RelayServerIp [relay server's IP] -RelayServerTKey [relay server's TKey]` - run client `client -IsRelay -ServerIp [relay server's IP] -ServerTKey [relay server's TKey] -ExitServerClientId [exit server's ClientId] -ExitServerToken [exit server's TKey]` + + +This product includes GeoLite2 data created by MaxMind, available from +https://www.maxmind.com. \ No newline at end of file diff --git a/src/github.com/tachyon-protocol/udw b/src/github.com/tachyon-protocol/udw index c7ec65b..d9539c8 160000 --- a/src/github.com/tachyon-protocol/udw +++ b/src/github.com/tachyon-protocol/udw @@ -1 +1 @@ -Subproject commit c7ec65b78eb6e4ab27030548508a8b26d429242e +Subproject commit d9539c8521bb1b7b0c0f1cefc3d3fa87f3bd1736 diff --git a/src/tachyonVpnServer/server.go b/src/tachyonVpnServer/server.go index 57ac108..f23faf5 100644 --- a/src/tachyonVpnServer/server.go +++ b/src/tachyonVpnServer/server.go @@ -20,6 +20,10 @@ import ( "tachyonVpnProtocol" "time" "tyTls" + "github.com/tachyon-protocol/udw/udwIpToCountryV2" + "github.com/tachyon-protocol/udw/udwStrings" + "strings" + "github.com/tachyon-protocol/udw/udwClose" ) type ServerRunReq struct { @@ -29,6 +33,7 @@ type ServerRunReq struct { RelayServerChk string SelfTKey string + BlockCountryCodeListS string // empty string do not block any country code, look like "KP,IR,RU" } type Server struct { @@ -38,8 +43,9 @@ type Server struct { vpnIpList [maxCountVpnIp]*vpnClient nextVpnIpIndex int tun *udwTapTun.TunTapObj - req ServerRunReq + blockCountryCodeList []string + closer udwClose.Closer } func (s *Server) Run(req ServerRunReq) { @@ -66,7 +72,9 @@ func (s *Server) Run(req ServerRunReq) { udwErr.PanicIfErrorMsg(errMsg) fmt.Println("ServerChk: "+tyTls.MustHashChkFromTlsCert(tlsServerCert)) fmt.Println("Server started ✔") - + if s.req.BlockCountryCodeListS!=""{ + s.blockCountryCodeList = strings.Split(s.req.BlockCountryCodeListS,",") + } //read thread from TUN go func() { @@ -168,18 +176,32 @@ func (s *Server) Run(req ServerRunReq) { } }() } else { - ln, err := net.Listen("tcp", ":"+strconv.Itoa(tachyonVpnProtocol.VpnPort)) - udwErr.PanicIfError(err) - go func() { - for { - conn, err := ln.Accept() - udwErr.PanicIfError(err) - conn = tls.Server(conn, sTlsConfig) - go s.clientTcpConnHandle(conn) + closerFn:=udwNet.TcpNewListener(":"+strconv.Itoa(tachyonVpnProtocol.VpnPort),func(conn net.Conn){ + if s.clientConnFilter(conn)==false{ + conn.Close() + return } - }() + conn = tls.Server(conn, sTlsConfig) + s.clientTcpConnHandle(conn) + }) + s.closer.AddOnClose(closerFn) } udwConsole.WaitForExit() + s.closer.Close() +} + +// return true as pass +func (s *Server) clientConnFilter(connToClient net.Conn) bool{ + if len(s.blockCountryCodeList)>0{ + ip,_,errMsg:=udwNet.GetIpAndPortFromNetAddr(connToClient.RemoteAddr()) + if errMsg==""{ + cc:=udwIpToCountryV2.MustGetCountryIsoCode(ip) + if cc!="" && udwStrings.IsInSlice(s.blockCountryCodeList,cc){ + return false + } + } + } + return true } func (s *Server) clientTcpConnHandle(connToClient net.Conn) { From a00ba9042748416103fd5b6a7219424bda21d424 Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Sat, 7 Dec 2019 07:41:00 +0000 Subject: [PATCH 5/7] --- src/github.com/tachyon-protocol/udw | 2 +- src/make/tpl/main.go | 8 ++ src/tachyonVpnInfoServer/main.go | 121 ------------------ .../tachyonVpnInfoClient/model.go | 6 - src/tachyonVpnInfoServer/vpnNode.go | 28 ---- src/tachyonVpnRouteServer/main.go | 116 +++++++++++++++++ .../tachyonVpnRouteClient}/main.go | 2 +- .../tachyonVpnRouteClient}/main_test.go | 2 +- .../tachyonVpnRouteClient/model.go | 14 ++ .../tachyonVpnRoute_Build/main.go | 63 +++++++++ src/tachyonVpnRouteServer/vpnNode.go | 67 ++++++++++ 11 files changed, 271 insertions(+), 158 deletions(-) create mode 100644 src/make/tpl/main.go delete mode 100644 src/tachyonVpnInfoServer/main.go delete mode 100644 src/tachyonVpnInfoServer/tachyonVpnInfoClient/model.go delete mode 100644 src/tachyonVpnInfoServer/vpnNode.go create mode 100644 src/tachyonVpnRouteServer/main.go rename src/{tachyonVpnInfoServer/tachyonVpnInfoClient => tachyonVpnRouteServer/tachyonVpnRouteClient}/main.go (98%) rename src/{tachyonVpnInfoServer/tachyonVpnInfoClient => tachyonVpnRouteServer/tachyonVpnRouteClient}/main_test.go (94%) create mode 100644 src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go create mode 100644 src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go create mode 100644 src/tachyonVpnRouteServer/vpnNode.go diff --git a/src/github.com/tachyon-protocol/udw b/src/github.com/tachyon-protocol/udw index d9539c8..db26860 160000 --- a/src/github.com/tachyon-protocol/udw +++ b/src/github.com/tachyon-protocol/udw @@ -1 +1 @@ -Subproject commit d9539c8521bb1b7b0c0f1cefc3d3fa87f3bd1736 +Subproject commit db268604ff338dfbe596c9401fa7d5b91d9f245f diff --git a/src/make/tpl/main.go b/src/make/tpl/main.go new file mode 100644 index 0000000..b3aee51 --- /dev/null +++ b/src/make/tpl/main.go @@ -0,0 +1,8 @@ +package main + +import "github.com/tachyon-protocol/udw/udwBuild" + +func main(){ + udwBuild.MustBuild() +} + diff --git a/src/tachyonVpnInfoServer/main.go b/src/tachyonVpnInfoServer/main.go deleted file mode 100644 index 34ba03c..0000000 --- a/src/tachyonVpnInfoServer/main.go +++ /dev/null @@ -1,121 +0,0 @@ -package tachyonVpnInfoServer - -import ( - "github.com/tachyon-protocol/udw/udwSqlite3" - "sync" - "net/http" - "github.com/tachyon-protocol/udw/udwTlsSelfSignCertV2" - "fmt" - "github.com/tachyon-protocol/udw/udwErr" - "github.com/tachyon-protocol/udw/udwJson" - "net" - "time" - "github.com/tachyon-protocol/udw/udwTime" - "tachyonVpnInfoServer/tachyonVpnInfoClient" -) - -func ServerAsyncRun() func(){ - initDb() - s:=http.Server{ - Addr: ":443", - Handler: http.HandlerFunc(serverHandler), - TLSConfig: udwTlsSelfSignCertV2.GetTlsConfig(), - } - wg:=sync.WaitGroup{} - wg.Add(1) - go func(){ - err := s.ListenAndServeTLS("","") - wg.Done() - if err!=nil && err!=http.ErrServerClosed{ - fmt.Println("hguuwustns",err) - return - } - }() - return func(){ - s.Close() - wg.Wait() - } -} - -func serverHandler(w http.ResponseWriter,req *http.Request){ - errMsg:=udwErr.PanicToErrorMsg(func(){ - if req.Method!=http.MethodPost{ - http.NotFound(w,req) - return - } - values:=req.URL.Query() - n:=values.Get("n") - switch n { - case "RegisterAsVpnNode": - - case "RegisterFromIpAsVpnNode": - fromIp:=getClientIpStringIgnoreError(req) - serverRpcObj{}.RegisterFromIpAsVpnNode(fromIp) - case "UnregisterFromIpAsVpnNode": - fromIp:=getClientIpStringIgnoreError(req) - serverRpcObj{}.UnregisterFromIpAsVpnNode(fromIp) - case "GetVpnNodeIpList": - ipList:=serverRpcObj{}.GetVpnNodeIpList() - w.Write(udwJson.MustMarshal(ipList)) - default: - http.NotFound(w,req) - return - } - }) - if errMsg!=""{ - w.WriteHeader(500) - w.Write([]byte(errMsg)) - } - return -} - -type serverRpcObj struct{} - -func (serverRpcObj) RegisterFromIpAsVpnNode(fromIp string){ - startTime:=time.Now() - getDb().MustSet(k1VpnNodeIp,fromIp,udwTime.MustDbTimeGetStringFromObj(startTime)) -} -func (serverRpcObj) UnregisterFromIpAsVpnNode(fromIp string){ - getDb().MustDelete(k1VpnNodeIp,fromIp) -} -func (serverRpcObj) GetVpnNodeIpList() []string{ - outputList:=[]string{} - startTime:=time.Now() - getDb().MustGetRangeCallback(udwSqlite3.GetRangeReq{ - K1: k1VpnNodeIp, - Limit: 1000, - },func(k string,v string){ - t:=udwTime.MustDbTimeGetObjFromString(v) - if t.Before(startTime.Add(-time.Second*30)){ - getDb().MustDelete(k1VpnNodeIp,k) - }else{ - outputList = append(outputList,k) - } - }) - return outputList -} - -var gSqlite3Db *udwSqlite3.Db -var gSqlite3DbOnce sync.Once - -func initDb(){ - gSqlite3DbOnce.Do(func(){ - gSqlite3Db = udwSqlite3.MustNewDb(udwSqlite3.NewDbRequest{ - FilePath: "/usr/local/var/tachyonVpnInfoServer.sqlite3", - EmptyDatabaseIfDatabaseCorrupt: true, - }) - }) -} - -func getDb() *udwSqlite3.Db{ - return gSqlite3Db -} - -func getClientIpStringIgnoreError(req *http.Request) string{ - host, _, err := net.SplitHostPort(req.RemoteAddr) - if err == nil { - return host - } - return "" -} - diff --git a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/model.go b/src/tachyonVpnInfoServer/tachyonVpnInfoClient/model.go deleted file mode 100644 index f8fb64c..0000000 --- a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/model.go +++ /dev/null @@ -1,6 +0,0 @@ -package tachyonVpnInfoClient - -type RegisterAsVpnNodeReq struct{ - Ip string `json:",omitempty"` - ServerCertPem string `json:",omitempty"` -} \ No newline at end of file diff --git a/src/tachyonVpnInfoServer/vpnNode.go b/src/tachyonVpnInfoServer/vpnNode.go deleted file mode 100644 index f57da0b..0000000 --- a/src/tachyonVpnInfoServer/vpnNode.go +++ /dev/null @@ -1,28 +0,0 @@ -package tachyonVpnInfoServer - -import ( - "tachyonVpnInfoServer/tachyonVpnInfoClient" - "time" - "github.com/tachyon-protocol/udw/udwJson" - "tachyonVpnClient" -) - -func (serverRpcObj) RegisterAsVpnNode(req tachyonVpnInfoClient.RegisterAsVpnNodeReq){ - startTime:=time.Now() - thisNode:=ServerNode{ - Ip: req.Ip, - ServerCertPem: req.ServerCertPem, - UpdateTime: startTime, - } - tachyonVpnClient.Ping(tachyonVpnClient.PingReq{ - - }) - getDb().MustSet(k1VpnNodeIp,req.Ip,udwJson.MustMarshalToString(thisNode)) -} - -type ServerNode struct{ - Ip string - ServerCertPem string - UpdateTime time.Time -} -const k1VpnNodeIp = "k1VpnNodeIp2" \ No newline at end of file diff --git a/src/tachyonVpnRouteServer/main.go b/src/tachyonVpnRouteServer/main.go new file mode 100644 index 0000000..5407d5f --- /dev/null +++ b/src/tachyonVpnRouteServer/main.go @@ -0,0 +1,116 @@ +package tachyonVpnRouteServer + +import ( + "github.com/tachyon-protocol/udw/udwSqlite3" + "sync" + "net/http" + "net" +) + +func ServerAsyncRun() func(){ + initDb() + initGcVpnNode() + + //s:=http.Server{ + // Addr: ":443", + // Handler: http.HandlerFunc(serverHandler), + // TLSConfig: udwTlsSelfSignCertV2.GetTlsConfig(), + //} + //wg:=sync.WaitGroup{} + //wg.Add(1) + //go func(){ + // err := s.ListenAndServeTLS("","") + // wg.Done() + // if err!=nil && err!=http.ErrServerClosed{ + // fmt.Println("hguuwustns",err) + // return + // } + //}() + //return func(){ + // s.Close() + // wg.Wait() + //} +} + +//func serverHandler(w http.ResponseWriter,req *http.Request){ +// errMsg:=udwErr.PanicToErrorMsg(func(){ +// if req.Method!=http.MethodPost{ +// http.NotFound(w,req) +// return +// } +// values:=req.URL.Query() +// n:=values.Get("n") +// switch n { +// case "RegisterAsVpnNode": +// +// case "RegisterFromIpAsVpnNode": +// fromIp:=getClientIpStringIgnoreError(req) +// serverRpcObj{}.RegisterFromIpAsVpnNode(fromIp) +// case "UnregisterFromIpAsVpnNode": +// fromIp:=getClientIpStringIgnoreError(req) +// serverRpcObj{}.UnregisterFromIpAsVpnNode(fromIp) +// case "GetVpnNodeIpList": +// ipList:=serverRpcObj{}.GetVpnNodeIpList() +// w.Write(udwJson.MustMarshal(ipList)) +// default: +// http.NotFound(w,req) +// return +// } +// }) +// if errMsg!=""{ +// w.WriteHeader(500) +// w.Write([]byte(errMsg)) +// } +// return +//} + +type serverRpcObj struct{} + +//func (serverRpcObj) RegisterFromIpAsVpnNode(fromIp string){ +// startTime:=time.Now() +// getDb().MustSet(k1VpnNodeIp,fromIp,udwTime.MustDbTimeGetStringFromObj(startTime)) +//} +//func (serverRpcObj) UnregisterFromIpAsVpnNode(fromIp string){ +// getDb().MustDelete(k1VpnNodeIp,fromIp) +//} +//func (serverRpcObj) GetVpnNodeIpList() []string{ +// outputList:=[]string{} +// startTime:=time.Now() +// getDb().MustGetRangeCallback(udwSqlite3.GetRangeReq{ +// K1: k1VpnNodeIp, +// Limit: 1000, +// },func(k string,v string){ +// t:=udwTime.MustDbTimeGetObjFromString(v) +// if t.Before(startTime.Add(-time.Second*30)){ +// getDb().MustDelete(k1VpnNodeIp,k) +// }else{ +// outputList = append(outputList,k) +// } +// }) +// return outputList +//} + +var gSqlite3Db *udwSqlite3.Db +var gSqlite3DbOnce sync.Once + +func initDb(){ + gSqlite3DbOnce.Do(func(){ + gSqlite3Db = udwSqlite3.MustNewDb(udwSqlite3.NewDbRequest{ + FilePath: "/usr/local/var/tachyonVpnInfoServer.sqlite3", + EmptyDatabaseIfDatabaseCorrupt: true, + }) + }) +} + +func getDb() *udwSqlite3.Db{ + return gSqlite3Db +} + +func getClientIpStringIgnoreError(req *http.Request) string{ + host, _, err := net.SplitHostPort(req.RemoteAddr) + if err == nil { + return host + } + return "" +} + diff --git a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/main.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go similarity index 98% rename from src/tachyonVpnInfoServer/tachyonVpnInfoClient/main.go rename to src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go index 021cbc2..a571413 100644 --- a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/main.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go @@ -1,4 +1,4 @@ -package tachyonVpnInfoClient +package tachyonVpnRouteClient import ( "net/http" diff --git a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/main_test.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go similarity index 94% rename from src/tachyonVpnInfoServer/tachyonVpnInfoClient/main_test.go rename to src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go index 8080665..6d10772 100644 --- a/src/tachyonVpnInfoServer/tachyonVpnInfoClient/main_test.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go @@ -1,4 +1,4 @@ -package tachyonVpnInfoClient +package tachyonVpnRouteClient import ( "testing" diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go new file mode 100644 index 0000000..1631071 --- /dev/null +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go @@ -0,0 +1,14 @@ +package tachyonVpnRouteClient + +import "time" + +type RegisterAsVpnNodeReq struct{ + Ip string `json:",omitempty"` + ServerChk string `json:",omitempty"` +} + +type VpnNode struct{ + Ip string `json:",omitempty"` + ServerChk string `json:",omitempty"` + UpdateTime time.Time +} \ No newline at end of file diff --git a/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go new file mode 100644 index 0000000..c920461 --- /dev/null +++ b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go @@ -0,0 +1,63 @@ +package tachyonVpnRoute_Build + +import ( + "github.com/tachyon-protocol/udw/udwRpc2/udwRpc2Builder" +) + +func UdwBuild(){ + udwRpc2Builder.Generate(udwRpc2Builder.GenerateReq{ + RpcDefine: getRpcService(), + TargetPkgPath: "tachyonVpnRouteServer", + Prefix: "Rpc", + TargetFilePath: "src/tachyonVpnRouteServer/rpc.go", + GoFmt: true, + }) +} + +func getRpcService() udwRpc2Builder.RpcService { + return udwRpc2Builder.RpcService{ + List: []udwRpc2Builder.RpcApi{ + { + Name: "VpnNodeRegister", + InputParameterList: []udwRpc2Builder.RpcParameter{ + { + Type: udwRpc2Builder.RpcType{ + Kind: udwRpc2Builder.RpcTypeKindNamedStruct, + StructName: "PeerIp", + GoPkg: "github.com/tachyon-protocol/udw/udwRpc2", + }, + }, + { + Type: udwRpc2Builder.RpcType{ + Kind: udwRpc2Builder.RpcTypeKindNamedStruct, + StructName: "VpnNode", + GoPkg: "tachyonVpnRouteServer/tachyonVpnRouteClient", + }, + }, + }, + OutputParameterList: []udwRpc2Builder.RpcParameter{ + { + Type: udwRpc2Builder.RpcType{ + Kind: udwRpc2Builder.RpcTypeKindString, + }, + }, + }, + }, + { + Name: "VpnNodeList", + OutputParameterList: []udwRpc2Builder.RpcParameter{ + { + Type: udwRpc2Builder.RpcType{ + Kind: udwRpc2Builder.RpcTypeKindSlice, + Elem: &udwRpc2Builder.RpcType{ + Kind: udwRpc2Builder.RpcTypeKindNamedStruct, + StructName: "VpnNode", + GoPkg: "tachyonVpnRouteServer/tachyonVpnRouteClient", + }, + }, + }, + }, + }, + }, + } +} \ No newline at end of file diff --git a/src/tachyonVpnRouteServer/vpnNode.go b/src/tachyonVpnRouteServer/vpnNode.go new file mode 100644 index 0000000..fe42e4d --- /dev/null +++ b/src/tachyonVpnRouteServer/vpnNode.go @@ -0,0 +1,67 @@ +package tachyonVpnRouteServer + +import ( + "tachyonVpnRouteServer/tachyonVpnRouteClient" + "time" + "github.com/tachyon-protocol/udw/udwJson" + "tachyonVpnClient" + "github.com/tachyon-protocol/udw/udwRpc2" + "github.com/tachyon-protocol/udw/udwSqlite3" +) + +func (serverRpcObj) VpnNodeRegister(clientIp udwRpc2.PeerIp,thisNode tachyonVpnRouteClient.VpnNode) (errMsg string){ + startTime:=time.Now().UTC() + thisNode.UpdateTime = startTime.Truncate(time.Second) + if thisNode.Ip==""{ + thisNode.Ip = clientIp.Ip + } + err:=tachyonVpnClient.Ping(tachyonVpnClient.PingReq{ + Ip: thisNode.Ip, + ServerChk: thisNode.ServerChk, + }) + if err!=nil{ + return "f3pbhbjveg "+err.Error() + } + getDb().MustSet(k1VpnNodeIp,thisNode.Ip,udwJson.MustMarshalToString(thisNode)) + return "" +} + +func (serverRpcObj) VpnNodeList() []tachyonVpnRouteClient.VpnNode{ + outList:=[]tachyonVpnRouteClient.VpnNode{} + getDb().MustGetRangeCallback(udwSqlite3.GetRangeReq{ + K1: k1VpnNodeIp, + },func(key string, value string){ + var thisNode tachyonVpnRouteClient.VpnNode + udwJson.MustUnmarshalFromString(value,&thisNode) + if isNodeTimeout(thisNode)==false{ + outList = append(outList,thisNode) + }else{ + getDb().MustDeleteWithKv(k1VpnNodeIp,key,value) + } + }) + return outList +} + +func initGcVpnNode(){ + go func(){ + for{ + time.Sleep(k1VpnNodeTtl) + getDb().MustGetRangeCallback(udwSqlite3.GetRangeReq{ + K1: k1VpnNodeIp, + },func(key string, value string) { + var thisNode tachyonVpnRouteClient.VpnNode + udwJson.MustUnmarshalFromString(value,&thisNode) + if isNodeTimeout(thisNode){ + getDb().MustDeleteWithKv(k1VpnNodeIp,key,value) + } + }) + } + }() +} + +func isNodeTimeout(thisNode tachyonVpnRouteClient.VpnNode) bool{ + return time.Now().Add(-k1VpnNodeTtl).After(thisNode.UpdateTime) +} + +const k1VpnNodeIp = "k1VpnNodeIp2" +const k1VpnNodeTtl = time.Minute \ No newline at end of file From 90623a72d6512b8c5051caa5f9a197f1ab878411 Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Mon, 9 Dec 2019 02:35:21 +0000 Subject: [PATCH 6/7] --- src/github.com/tachyon-protocol/udw | 2 +- src/make/ci/main.go | 1 + src/make/routeServer/main.go | 10 ++ src/make/sshDeploy/main.go | 17 +++- src/make/tmp/main.go | 14 ++- src/tachyonVpnClient/client.go | 61 ++++++++++-- src/tachyonVpnProtocol/main.go | 2 + src/tachyonVpnRouteServer/main.go | 6 +- src/tachyonVpnRouteServer/rpc.go | 73 ++++++++++++++ .../{main.go => main.go.bak} | 0 .../{main_test.go => main_test.go.bak} | 0 .../tachyonVpnRouteClient/model.go | 8 +- .../tachyonVpnRouteClient/rpc.go | 96 +++++++++++++++++++ .../tachyonVpnRoute_Build/main.go | 13 +++ src/tachyonVpnServer/server.go | 23 ++++- 15 files changed, 309 insertions(+), 17 deletions(-) create mode 100644 src/make/routeServer/main.go create mode 100644 src/tachyonVpnRouteServer/rpc.go rename src/tachyonVpnRouteServer/tachyonVpnRouteClient/{main.go => main.go.bak} (100%) rename src/tachyonVpnRouteServer/tachyonVpnRouteClient/{main_test.go => main_test.go.bak} (100%) create mode 100644 src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go diff --git a/src/github.com/tachyon-protocol/udw b/src/github.com/tachyon-protocol/udw index db26860..028859c 160000 --- a/src/github.com/tachyon-protocol/udw +++ b/src/github.com/tachyon-protocol/udw @@ -1 +1 @@ -Subproject commit db268604ff338dfbe596c9401fa7d5b91d9f245f +Subproject commit 028859c1eb31ae053ba7c248b8f7d975e7497d9b diff --git a/src/make/ci/main.go b/src/make/ci/main.go index ddec587..b8658f4 100644 --- a/src/make/ci/main.go +++ b/src/make/ci/main.go @@ -10,6 +10,7 @@ import ( func main(){ tryGoInstall("make/client") tryGoInstall("make/server") + tryGoInstall("tachyonVpnRouteServer") thisPath:=udwFile.MustGetFullPath("src/github.com/tachyon-protocol/udw") dirSet:=map[string]struct{}{} for _,fullpath:=range udwFile.MustGetAllFiles(thisPath){ diff --git a/src/make/routeServer/main.go b/src/make/routeServer/main.go new file mode 100644 index 0000000..d37ff7c --- /dev/null +++ b/src/make/routeServer/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "github.com/tachyon-protocol/udw/udwConsole" + "tachyonVpnRouteServer" +) + +func main(){ + udwConsole.MustRunCommandLineFromFuncV2(tachyonVpnRouteServer.RouteServerRunCmd) +} diff --git a/src/make/sshDeploy/main.go b/src/make/sshDeploy/main.go index 95e1b17..62d3f95 100644 --- a/src/make/sshDeploy/main.go +++ b/src/make/sshDeploy/main.go @@ -13,6 +13,7 @@ func main() { Ip string PkgPath string Command string + ClientKeyFilePath string }) { const ( _os = `linux` @@ -26,8 +27,20 @@ func main() { }) pkgName := filepath.Base(req.PkgPath) fmt.Println("build successfully", pkgName, _os, "/", arch) - udwSsh.MustScpToRemoteDefault(req.Ip, resp.GetOutputExeFilePath(), "/tmp/"+pkgName) + + udwSsh.MustScpToRemote(&udwSsh.RemoteServer{ + Ip: req.Ip, + LocalPath: resp.GetOutputExeFilePath(), + RemotePath: "/tmp/"+pkgName, + IsScp: true, + ClientKeyFilePath: req.ClientKeyFilePath, + }) + //udwSsh.MustScpToRemoteDefault(req.Ip, resp.GetOutputExeFilePath(), "/tmp/"+pkgName) //udwSsh.MustRpcSshDefault(serverIp, "mv /tmp/"+pkgName+" /usr/local/bin/"+pkgName+";killall "+pkgName+";setsid "+pkgName+" >> /tmp/server.log 2>&1") - udwSsh.MustRpcSshDefault(req.Ip, "mv /tmp/"+pkgName+" /usr/local/bin/"+pkgName+";killall "+pkgName+";sleep 2;"+req.Command) + udwSsh.MustRpcSsh(udwSsh.RemoteServer{ + Ip: req.Ip, + ClientKeyFilePath: req.ClientKeyFilePath, + },"mv /tmp/"+pkgName+" /usr/local/bin/"+pkgName+";killall "+pkgName+";sleep 2;"+req.Command) + //udwSsh.MustRpcSshDefault(req.Ip, "mv /tmp/"+pkgName+" /usr/local/bin/"+pkgName+";killall "+pkgName+";sleep 2;"+req.Command) }) } diff --git a/src/make/tmp/main.go b/src/make/tmp/main.go index 073214d..5384b5e 100644 --- a/src/make/tmp/main.go +++ b/src/make/tmp/main.go @@ -1,7 +1,17 @@ package main -import "github.com/tachyon-protocol/udw/udwRpc2/udwRpc2Tester" +import ( + "tachyonVpnRouteServer/tachyonVpnRouteClient" + "tachyonVpnProtocol" + "github.com/tachyon-protocol/udw/udwDebug" +) func main(){ - udwRpc2Tester.BuildAndTest() + //udwRpc2Tester.BuildAndTest() + routeC:=tachyonVpnRouteClient.Rpc_NewClient(tachyonVpnProtocol.PublicRouteServerAddr) + list,rpcErr:=routeC.VpnNodeList() + if rpcErr!=nil{ + panic(rpcErr.Error()) + } + udwDebug.Println(list) } diff --git a/src/tachyonVpnClient/client.go b/src/tachyonVpnClient/client.go index a2ef2ff..9272ce4 100644 --- a/src/tachyonVpnClient/client.go +++ b/src/tachyonVpnClient/client.go @@ -21,6 +21,9 @@ import ( "tachyonVpnProtocol" "time" "tyTls" + "tachyonVpnRouteServer/tachyonVpnRouteClient" +// "github.com/tachyon-protocol/udw/udwClose" + "fmt" ) type RunReq struct { @@ -32,6 +35,7 @@ type RunReq struct { ExitServerTKey string //required when IsRelay is true ServerChk string // if it is "", it will use InsecureSkipVerify + DisableUsePublicRouteServer bool } type Client struct { @@ -50,21 +54,22 @@ func (c *Client) Run(req RunReq) { tyTls.EnableTlsVersion13() c.clientId = tachyonVpnProtocol.GetClientId() c.clientIdToExitServer = c.clientId - if req.IsRelay { + if c.req.IsRelay { c.clientIdToExitServer = tachyonVpnProtocol.GetClientId() - if req.ExitServerClientId == 0 { + if c.req.ExitServerClientId == 0 { panic("ExitServerClientId can be empty when use relay mode") } } - tun, err := createTun(req.ServerIp) + c.tryUseRouteServer() + tun, err := createTun(c.req.ServerIp) udwErr.PanicIfError(err) //err = c.connect() - if req.ServerChk==""{ + if c.req.ServerChk==""{ c.tlsConfig = newInsecureClientTlsConfig() }else{ var errMsg string c.tlsConfig,errMsg = tyTls.NewClientTlsConfigWithChk(tyTls.NewClientTlsConfigWithChkReq{ - ServerChk: req.ServerChk, + ServerChk: c.req.ServerChk, }) udwErr.PanicIfErrorMsg(errMsg) } @@ -74,7 +79,7 @@ func (c *Client) Run(req RunReq) { vpnPacket := &tachyonVpnProtocol.VpnPacket{ Cmd: tachyonVpnProtocol.CmdData, ClientIdSender: c.clientIdToExitServer, - ClientIdReceiver: req.ExitServerClientId, + ClientIdReceiver: c.req.ExitServerClientId, } buf := make([]byte, 16*1024) bufW := udwBytes.NewBufWriter(nil) @@ -212,3 +217,47 @@ func newInsecureClientTlsConfig() *tls.Config { MinVersion: tls.VersionTLS12, } } + +func (c *Client) tryUseRouteServer(){ + if c.req.ServerIp==""{ + if c.req.DisableUsePublicRouteServer{ + panic("need config ServerIp") + }else{ + fmt.Println("start mulit ping 1") + routeC:=tachyonVpnRouteClient.Rpc_NewClient(tachyonVpnProtocol.PublicRouteServerAddr) + list,rpcErr:=routeC.VpnNodeList() + if rpcErr!=nil{ + panic(rpcErr.Error()) + } + fmt.Println("start mulit ping 2") + locker :=sync.Mutex{} + var fastNode tachyonVpnRouteClient.VpnNode + wg:=sync.WaitGroup{} + for _,node:=range list{ + node:=node + wg.Add(1) + go func(){ + err:=Ping(PingReq{ + Ip: node.Ip, + ServerChk: node.ServerChk, + }) + if err==nil{ + locker.Lock() + if fastNode.Ip==""{ + fastNode = node + } + locker.Unlock() + } + wg.Done() + }() + } + wg.Wait() + if fastNode.Ip==""{ + panic("all ping lost") + } + c.req.ServerIp = fastNode.Ip + c.req.ServerChk = fastNode.ServerChk + fmt.Println("ping to get ip ["+c.req.ServerIp+"]") + } + } +} \ No newline at end of file diff --git a/src/tachyonVpnProtocol/main.go b/src/tachyonVpnProtocol/main.go index b8a59c3..e2afef2 100644 --- a/src/tachyonVpnProtocol/main.go +++ b/src/tachyonVpnProtocol/main.go @@ -24,6 +24,8 @@ const ( CmdKeepAlive byte = 5 ) +const PublicRouteServerAddr = "35.223.105.46:24587" + type VpnPacket struct { Cmd byte ClientIdSender uint64 diff --git a/src/tachyonVpnRouteServer/main.go b/src/tachyonVpnRouteServer/main.go index 5407d5f..13d8baf 100644 --- a/src/tachyonVpnRouteServer/main.go +++ b/src/tachyonVpnRouteServer/main.go @@ -5,12 +5,16 @@ import ( "sync" "net/http" "net" + "github.com/tachyon-protocol/udw/udwConsole" ) -func ServerAsyncRun() func(){ +func RouteServerRunCmd() { initDb() initGcVpnNode() + closer:=Rpc_RunServer(":24587") + udwConsole.WaitForExit() + closer() //s:=http.Server{ // Addr: ":443", // Handler: http.HandlerFunc(serverHandler), diff --git a/src/tachyonVpnRouteServer/rpc.go b/src/tachyonVpnRouteServer/rpc.go new file mode 100644 index 0000000..c8804fa --- /dev/null +++ b/src/tachyonVpnRouteServer/rpc.go @@ -0,0 +1,73 @@ +package tachyonVpnRouteServer + +import ( + "github.com/tachyon-protocol/udw/udwRpc2" + "tachyonVpnRouteServer/tachyonVpnRouteClient" +) + +func Rpc_RunServer(addr string) (closer func()) { + s := serverRpcObj{} + sh := udwRpc2.NewServerHub(udwRpc2.ServerReq{ + Addr: addr, + Handler: func(ctx *udwRpc2.ReqCtx) { + var fnId uint64 + var errMsg string + fnId, errMsg = ctx.GetReader().ReadUvarint() + if errMsg != "" { + return + } + panicErrMsg := udwRpc2.PanicToErrMsg(func() { + switch fnId { + case 1: + tmp_1 := udwRpc2.PeerIp{ctx.GetPeerIp()} + var tmp_2 tachyonVpnRouteClient.VpnNode + errMsg = ctx.GetReader().ReadValue(&tmp_2) + if errMsg != "" { + return + } + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + return + } + tmp_3 := s.VpnNodeRegister(tmp_1, tmp_2) + ctx.GetWriter().WriteString("") + errMsg = ctx.GetWriter().WriteValue(tmp_3) + if errMsg != "" { + return + } + ctx.GetWriter().WriteArrayEnd() + errMsg = ctx.GetWriter().Flush() + if errMsg != "" { + return + } + case 2: + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + return + } + tmp_4 := s.VpnNodeList() + ctx.GetWriter().WriteString("") + errMsg = ctx.GetWriter().WriteValue(tmp_4) + if errMsg != "" { + return + } + ctx.GetWriter().WriteArrayEnd() + errMsg = ctx.GetWriter().Flush() + if errMsg != "" { + return + } + default: + } + }) + if panicErrMsg != "" { + ctx.GetWriter().WriteString(panicErrMsg) + ctx.GetWriter().WriteArrayEnd() + errMsg = ctx.GetWriter().Flush() + if errMsg != "" { + return + } + } + }, + }) + return sh.Close +} diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go.bak similarity index 100% rename from src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go rename to src/tachyonVpnRouteServer/tachyonVpnRouteClient/main.go.bak diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go.bak similarity index 100% rename from src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go rename to src/tachyonVpnRouteServer/tachyonVpnRouteClient/main_test.go.bak diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go index 1631071..49d4547 100644 --- a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/model.go @@ -2,10 +2,10 @@ package tachyonVpnRouteClient import "time" -type RegisterAsVpnNodeReq struct{ - Ip string `json:",omitempty"` - ServerChk string `json:",omitempty"` -} +//type RegisterAsVpnNodeReq struct{ +// Ip string `json:",omitempty"` +// ServerChk string `json:",omitempty"` +//} type VpnNode struct{ Ip string `json:",omitempty"` diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go new file mode 100644 index 0000000..9ac4774 --- /dev/null +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go @@ -0,0 +1,96 @@ +package tachyonVpnRouteClient + +import ( + "github.com/tachyon-protocol/udw/udwRpc2" +) + +func Rpc_NewClient(addr string) *Rpc_Client { + c := udwRpc2.NewClientHub(udwRpc2.ClientReq{ + Addr: addr, + }) + return &Rpc_Client{ + ch: c, + } +} + +type Rpc_Client struct { + ch *udwRpc2.ClientHub +} + +func (c *Rpc_Client) VpnNodeRegister(fi2 VpnNode) (fo1 string, RpcErr *udwRpc2.RpcError) { + _networkErr := c.ch.RequestCb(func(ctx *udwRpc2.ReqCtx) { + ctx.GetWriter().WriteUvarint(1) + ctx.GetWriter().WriteValue(fi2) + ctx.GetWriter().WriteArrayEnd() + errMsg := ctx.GetWriter().Flush() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("dehqx82rjj " + errMsg) + return + } + var s string + errMsg = ctx.GetReader().ReadValue(&s) + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("ehtjkea4re " + errMsg) + return + } + if s != "" { + RpcErr = udwRpc2.NewOtherError(s) + ctx.GetReader().ReadArrayEnd() + return + } + errMsg = ctx.GetReader().ReadValue(&fo1) + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("kvkdcgtnk2 " + errMsg) + return + } + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("4b7rug5mf2 " + errMsg) + return + } + RpcErr = nil + return + }) + if _networkErr != "" { + RpcErr = udwRpc2.NewNetworkError("494fehebw6 " + _networkErr) + } + return +} +func (c *Rpc_Client) VpnNodeList() (fo1 []VpnNode, RpcErr *udwRpc2.RpcError) { + _networkErr := c.ch.RequestCb(func(ctx *udwRpc2.ReqCtx) { + ctx.GetWriter().WriteUvarint(2) + ctx.GetWriter().WriteArrayEnd() + errMsg := ctx.GetWriter().Flush() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("dehqx82rjj " + errMsg) + return + } + var s string + errMsg = ctx.GetReader().ReadValue(&s) + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("ehtjkea4re " + errMsg) + return + } + if s != "" { + RpcErr = udwRpc2.NewOtherError(s) + ctx.GetReader().ReadArrayEnd() + return + } + errMsg = ctx.GetReader().ReadValue(&fo1) + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("kvkdcgtnk2 " + errMsg) + return + } + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("4b7rug5mf2 " + errMsg) + return + } + RpcErr = nil + return + }) + if _networkErr != "" { + RpcErr = udwRpc2.NewNetworkError("494fehebw6 " + _networkErr) + } + return +} diff --git a/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go index c920461..a337f5f 100644 --- a/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go @@ -7,10 +7,23 @@ import ( func UdwBuild(){ udwRpc2Builder.Generate(udwRpc2Builder.GenerateReq{ RpcDefine: getRpcService(), + FromObjName: "serverRpcObj", + FromPkgPath: "tachyonVpnRouteServer", TargetPkgPath: "tachyonVpnRouteServer", Prefix: "Rpc", TargetFilePath: "src/tachyonVpnRouteServer/rpc.go", GoFmt: true, + DisableGenClient: true, + }) + udwRpc2Builder.Generate(udwRpc2Builder.GenerateReq{ + RpcDefine: getRpcService(), + FromObjName: "serverRpcObj", + FromPkgPath: "tachyonVpnRouteServer", + TargetPkgPath: "tachyonVpnRouteServer/tachyonVpnRouteClient", + Prefix: "Rpc", + TargetFilePath: "src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go", + GoFmt: true, + DisableGenServer: true, }) } diff --git a/src/tachyonVpnServer/server.go b/src/tachyonVpnServer/server.go index ffa29b3..8743632 100644 --- a/src/tachyonVpnServer/server.go +++ b/src/tachyonVpnServer/server.go @@ -21,6 +21,8 @@ import ( "github.com/tachyon-protocol/udw/udwStrings" "strings" "github.com/tachyon-protocol/udw/udwClose" + "tachyonVpnRouteServer/tachyonVpnRouteClient" + "time" ) type ServerRunReq struct { @@ -31,6 +33,7 @@ type ServerRunReq struct { SelfTKey string BlockCountryCodeListS string // empty string do not block any country code, look like "KP,IR,RU" + DisableRegisterRouteServer bool } type Server struct { @@ -71,11 +74,29 @@ func (s *Server) Run(req ServerRunReq) { ServerCert: *tlsServerCert, }) udwErr.PanicIfErrorMsg(errMsg) - fmt.Println("ServerChk: "+tyTls.MustHashChkFromTlsCert(tlsServerCert)) + serverChk:=tyTls.MustHashChkFromTlsCert(tlsServerCert) + fmt.Println("ServerChk: "+serverChk) fmt.Println("Server started ✔") if s.req.BlockCountryCodeListS!=""{ s.blockCountryCodeList = strings.Split(s.req.BlockCountryCodeListS,",") } + if s.req.DisableRegisterRouteServer==false{ + go func(){ + c:=tachyonVpnRouteClient.Rpc_NewClient(tachyonVpnProtocol.PublicRouteServerAddr) + for{ + err1,err2:=c.VpnNodeRegister(tachyonVpnRouteClient.VpnNode{ + ServerChk:serverChk, + }) + if err1!="" { + fmt.Println("4etcghekhj "+err1) + } + if err2!=nil{ + fmt.Println("yew68bub3a "+err2.Error()) + } + time.Sleep(time.Second*30) + } + }() + } //read thread from TUN go func() { From 9a007f56052812eb73f37c5db6864cada6ea84e2 Mon Sep 17 00:00:00 2001 From: Roger Carter Date: Mon, 9 Dec 2019 06:36:59 +0000 Subject: [PATCH 7/7] --- src/github.com/tachyon-protocol/udw | 2 +- src/make/tmp/main.go | 7 ++++ src/tachyonVpnClient/client.go | 2 -- src/tachyonVpnRouteServer/rpc.go | 12 +++++++ .../tachyonVpnRouteClient/rpc.go | 33 +++++++++++++++++++ .../tachyonVpnRoute_Build/main.go | 3 ++ src/tachyonVpnRouteServer/vpnNode.go | 2 ++ 7 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/github.com/tachyon-protocol/udw b/src/github.com/tachyon-protocol/udw index 028859c..55a55c3 160000 --- a/src/github.com/tachyon-protocol/udw +++ b/src/github.com/tachyon-protocol/udw @@ -1 +1 @@ -Subproject commit 028859c1eb31ae053ba7c248b8f7d975e7497d9b +Subproject commit 55a55c3f5064b8b8e3b3a9222dbfbf23f1622433 diff --git a/src/make/tmp/main.go b/src/make/tmp/main.go index 5384b5e..71b2ee6 100644 --- a/src/make/tmp/main.go +++ b/src/make/tmp/main.go @@ -4,11 +4,18 @@ import ( "tachyonVpnRouteServer/tachyonVpnRouteClient" "tachyonVpnProtocol" "github.com/tachyon-protocol/udw/udwDebug" + "fmt" ) func main(){ //udwRpc2Tester.BuildAndTest() routeC:=tachyonVpnRouteClient.Rpc_NewClient(tachyonVpnProtocol.PublicRouteServerAddr) + fmt.Println("start 1") + rpcErr:=routeC.Ping() + if rpcErr!=nil{ + panic(rpcErr.Error()) + } + fmt.Println("start 2") list,rpcErr:=routeC.VpnNodeList() if rpcErr!=nil{ panic(rpcErr.Error()) diff --git a/src/tachyonVpnClient/client.go b/src/tachyonVpnClient/client.go index 9272ce4..377a39f 100644 --- a/src/tachyonVpnClient/client.go +++ b/src/tachyonVpnClient/client.go @@ -223,13 +223,11 @@ func (c *Client) tryUseRouteServer(){ if c.req.DisableUsePublicRouteServer{ panic("need config ServerIp") }else{ - fmt.Println("start mulit ping 1") routeC:=tachyonVpnRouteClient.Rpc_NewClient(tachyonVpnProtocol.PublicRouteServerAddr) list,rpcErr:=routeC.VpnNodeList() if rpcErr!=nil{ panic(rpcErr.Error()) } - fmt.Println("start mulit ping 2") locker :=sync.Mutex{} var fastNode tachyonVpnRouteClient.VpnNode wg:=sync.WaitGroup{} diff --git a/src/tachyonVpnRouteServer/rpc.go b/src/tachyonVpnRouteServer/rpc.go index c8804fa..4e36de0 100644 --- a/src/tachyonVpnRouteServer/rpc.go +++ b/src/tachyonVpnRouteServer/rpc.go @@ -56,6 +56,18 @@ func Rpc_RunServer(addr string) (closer func()) { if errMsg != "" { return } + case 3: + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + return + } + s.Ping() + ctx.GetWriter().WriteString("") + ctx.GetWriter().WriteArrayEnd() + errMsg = ctx.GetWriter().Flush() + if errMsg != "" { + return + } default: } }) diff --git a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go index 9ac4774..5e61a44 100644 --- a/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRouteClient/rpc.go @@ -94,3 +94,36 @@ func (c *Rpc_Client) VpnNodeList() (fo1 []VpnNode, RpcErr *udwRpc2.RpcError) { } return } +func (c *Rpc_Client) Ping() (RpcErr *udwRpc2.RpcError) { + _networkErr := c.ch.RequestCb(func(ctx *udwRpc2.ReqCtx) { + ctx.GetWriter().WriteUvarint(3) + ctx.GetWriter().WriteArrayEnd() + errMsg := ctx.GetWriter().Flush() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("dehqx82rjj " + errMsg) + return + } + var s string + errMsg = ctx.GetReader().ReadValue(&s) + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("ehtjkea4re " + errMsg) + return + } + if s != "" { + RpcErr = udwRpc2.NewOtherError(s) + ctx.GetReader().ReadArrayEnd() + return + } + errMsg = ctx.GetReader().ReadArrayEnd() + if errMsg != "" { + RpcErr = udwRpc2.NewNetworkError("4b7rug5mf2 " + errMsg) + return + } + RpcErr = nil + return + }) + if _networkErr != "" { + RpcErr = udwRpc2.NewNetworkError("494fehebw6 " + _networkErr) + } + return +} diff --git a/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go index a337f5f..e1e7d72 100644 --- a/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go +++ b/src/tachyonVpnRouteServer/tachyonVpnRoute_Build/main.go @@ -71,6 +71,9 @@ func getRpcService() udwRpc2Builder.RpcService { }, }, }, + { + Name: "Ping", + }, }, } } \ No newline at end of file diff --git a/src/tachyonVpnRouteServer/vpnNode.go b/src/tachyonVpnRouteServer/vpnNode.go index fe42e4d..2c4c59d 100644 --- a/src/tachyonVpnRouteServer/vpnNode.go +++ b/src/tachyonVpnRouteServer/vpnNode.go @@ -42,6 +42,8 @@ func (serverRpcObj) VpnNodeList() []tachyonVpnRouteClient.VpnNode{ return outList } +func (serverRpcObj) Ping(){} + func initGcVpnNode(){ go func(){ for{