Skip to content

Commit

Permalink
report netinfo, improve relay model
Browse files Browse the repository at this point in the history
  • Loading branch information
hhd committed Dec 1, 2021
1 parent c83a57f commit a925073
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
34 changes: 34 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func TestAESCBC(t *testing.T) {
}

}

func TestNetInfo(t *testing.T) {
log.Println(netInfo())
}
22 changes: 11 additions & 11 deletions config.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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": ""
}
]
}
}
12 changes: 11 additions & 1 deletion p2pnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"errors"
"hash/crc64"
"math/big"
"net"
"time"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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"`
}

0 comments on commit a925073

Please sign in to comment.