diff --git a/common.go b/common.go index 8568eb6..99cb728 100644 --- a/common.go +++ b/common.go @@ -3,8 +3,12 @@ package main import ( "crypto/aes" "crypto/cipher" + "crypto/tls" + "encoding/json" "fmt" "net" + "net/http" + "time" ) func getmac(ip string) string { @@ -99,3 +103,33 @@ func decryptBytes(key []byte, out, in []byte, dataLen int) ([]byte, error) { mode.CryptBlocks(out[:dataLen], in[:dataLen]) return pkcs7UnPadding(out, dataLen) } + +// {240e:3b7:622:3440:59ad:7fa1:170c:ef7f 47924975352157270363627191692449083263 China CN 0xc0000965c8 Guangdong GD 0 Guangzhou 23.1167 113.25 Asia/Shanghai AS4134 Chinanet } +func netInfo() *NetInfo { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + // DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + // var d net.Dialer + // return d.DialContext(ctx, "tcp6", addr) + // }, + } + client := &http.Client{Transport: tr, Timeout: time.Second * 5} + r, err := client.Get("https://ifconfig.co/json") + if err != nil { + gLog.Println(LevelINFO, "netInfo error:", err) + return nil + } + defer r.Body.Close() + buf := make([]byte, 1024*64) + n, err := r.Body.Read(buf) + if err != nil { + gLog.Println(LevelINFO, "netInfo error:", err) + return nil + } + rsp := NetInfo{} + err = json.Unmarshal(buf[:n], &rsp) + if err != nil { + gLog.Printf(LevelERROR, "wrong NetInfo:%s", err) + } + return &rsp +} diff --git a/common_test.go b/common_test.go index c6444b7..39f40e3 100644 --- a/common_test.go +++ b/common_test.go @@ -39,3 +39,7 @@ func TestAESCBC(t *testing.T) { } } + +func TestNetInfo(t *testing.T) { + log.Println(netInfo()) +} diff --git a/config.json b/config.json index 71a764b..919fcd4 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,8 @@ { "network": { - "Node": "hhd1207-222", - "User": "tenderiron", - "Password": "13760636579", + "Node": "YOUR_NODE_NAME", + "User": "YOUR_USER_NAME", + "Password": "YOUR_PASSWORD", "ServerHost": "openp2p.cn", "ServerPort": 27182, "UDPPort1": 27182, @@ -11,21 +11,21 @@ "apps": [ { "Protocol": "tcp", - "SrcPort": 53389, - "PeerNode": "dell720-902", - "DstPort": 3389, - "DstHost": "10.1.6.36", + "SrcPort": 22, + "PeerNode": "YOURNODE1", + "DstPort": 22, + "DstHost": "127.0.0.1", "PeerUser": "", "PeerPassword": "" }, { "Protocol": "tcp", - "SrcPort": 22, - "PeerNode": "dell720-902", - "DstPort": 22, + "SrcPort": 50022, + "PeerNode": "YOURNODE2", + "DstPort": 50022, "DstHost": "127.0.0.1", "PeerUser": "", "PeerPassword": "" } ] -} \ No newline at end of file +} diff --git a/p2pnetwork.go b/p2pnetwork.go index aca9998..bdc89a7 100644 --- a/p2pnetwork.go +++ b/p2pnetwork.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "math/rand" + "net" "net/url" "os" "strings" @@ -417,13 +418,22 @@ func (pn *P2PNetwork) init() error { pn.config.mac = getmac(pn.config.localIP) pn.config.os = getOsName() + req := ReportBasic{ Mac: pn.config.mac, LanIP: pn.config.localIP, OS: pn.config.os, - IPv6: pn.config.ipv6, Version: OpenP2PVersion, } + rsp := netInfo() + gLog.Println(LevelINFO, rsp) + if rsp != nil && rsp.Country != "" { + if len(rsp.IP) == net.IPv6len { + pn.config.ipv6 = rsp.IP.String() + req.IPv6 = rsp.IP.String() + } + req.NetInfo = *rsp + } pn.write(MsgReport, MsgReportBasic, &req) gLog.Println(LevelINFO, "P2PNetwork init ok") break diff --git a/protocol.go b/protocol.go index c69e336..4cc575f 100644 --- a/protocol.go +++ b/protocol.go @@ -6,6 +6,8 @@ import ( "encoding/json" "errors" "hash/crc64" + "math/big" + "net" "time" ) @@ -250,11 +252,12 @@ type RelayHeartbeat struct { } type ReportBasic struct { - OS string `json:"os,omitempty"` - Mac string `json:"mac,omitempty"` - LanIP string `json:"lanIP,omitempty"` - IPv6 string `json:"IPv6,omitempty"` - Version string `json:"version,omitempty"` + OS string `json:"os,omitempty"` + Mac string `json:"mac,omitempty"` + LanIP string `json:"lanIP,omitempty"` + IPv6 string `json:"IPv6,omitempty"` + Version string `json:"version,omitempty"` + NetInfo NetInfo `json:"netInfo,omitempty"` } type ReportConnect struct { @@ -278,3 +281,22 @@ type UpdateInfo struct { ErrorDetail string `json:"errorDetail,omitempty"` Url string `json:"url,omitempty"` } + +type NetInfo struct { + IP net.IP `json:"ip"` + IPDecimal *big.Int `json:"ip_decimal"` + Country string `json:"country,omitempty"` + CountryISO string `json:"country_iso,omitempty"` + CountryEU *bool `json:"country_eu,omitempty"` + RegionName string `json:"region_name,omitempty"` + RegionCode string `json:"region_code,omitempty"` + MetroCode uint `json:"metro_code,omitempty"` + PostalCode string `json:"zip_code,omitempty"` + City string `json:"city,omitempty"` + Latitude float64 `json:"latitude,omitempty"` + Longitude float64 `json:"longitude,omitempty"` + Timezone string `json:"time_zone,omitempty"` + ASN string `json:"asn,omitempty"` + ASNOrg string `json:"asn_org,omitempty"` + Hostname string `json:"hostname,omitempty"` +}