mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-02-04 02:43:17 +08:00
report netinfo, improve relay model
This commit is contained in:
34
common.go
34
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
|
||||
}
|
||||
|
||||
@@ -39,3 +39,7 @@ func TestAESCBC(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNetInfo(t *testing.T) {
|
||||
log.Println(netInfo())
|
||||
}
|
||||
|
||||
22
config.json
22
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": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
32
protocol.go
32
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"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user