mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-04-26 03:20:24 +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 (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getmac(ip string) string {
|
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])
|
mode.CryptBlocks(out[:dataLen], in[:dataLen])
|
||||||
return pkcs7UnPadding(out, 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())
|
||||||
|
}
|
||||||
|
|||||||
20
config.json
20
config.json
@@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"network": {
|
"network": {
|
||||||
"Node": "hhd1207-222",
|
"Node": "YOUR_NODE_NAME",
|
||||||
"User": "tenderiron",
|
"User": "YOUR_USER_NAME",
|
||||||
"Password": "13760636579",
|
"Password": "YOUR_PASSWORD",
|
||||||
"ServerHost": "openp2p.cn",
|
"ServerHost": "openp2p.cn",
|
||||||
"ServerPort": 27182,
|
"ServerPort": 27182,
|
||||||
"UDPPort1": 27182,
|
"UDPPort1": 27182,
|
||||||
@@ -11,18 +11,18 @@
|
|||||||
"apps": [
|
"apps": [
|
||||||
{
|
{
|
||||||
"Protocol": "tcp",
|
"Protocol": "tcp",
|
||||||
"SrcPort": 53389,
|
"SrcPort": 22,
|
||||||
"PeerNode": "dell720-902",
|
"PeerNode": "YOURNODE1",
|
||||||
"DstPort": 3389,
|
"DstPort": 22,
|
||||||
"DstHost": "10.1.6.36",
|
"DstHost": "127.0.0.1",
|
||||||
"PeerUser": "",
|
"PeerUser": "",
|
||||||
"PeerPassword": ""
|
"PeerPassword": ""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Protocol": "tcp",
|
"Protocol": "tcp",
|
||||||
"SrcPort": 22,
|
"SrcPort": 50022,
|
||||||
"PeerNode": "dell720-902",
|
"PeerNode": "YOURNODE2",
|
||||||
"DstPort": 22,
|
"DstPort": 50022,
|
||||||
"DstHost": "127.0.0.1",
|
"DstHost": "127.0.0.1",
|
||||||
"PeerUser": "",
|
"PeerUser": "",
|
||||||
"PeerPassword": ""
|
"PeerPassword": ""
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -417,13 +418,22 @@ func (pn *P2PNetwork) init() error {
|
|||||||
|
|
||||||
pn.config.mac = getmac(pn.config.localIP)
|
pn.config.mac = getmac(pn.config.localIP)
|
||||||
pn.config.os = getOsName()
|
pn.config.os = getOsName()
|
||||||
|
|
||||||
req := ReportBasic{
|
req := ReportBasic{
|
||||||
Mac: pn.config.mac,
|
Mac: pn.config.mac,
|
||||||
LanIP: pn.config.localIP,
|
LanIP: pn.config.localIP,
|
||||||
OS: pn.config.os,
|
OS: pn.config.os,
|
||||||
IPv6: pn.config.ipv6,
|
|
||||||
Version: OpenP2PVersion,
|
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)
|
pn.write(MsgReport, MsgReportBasic, &req)
|
||||||
gLog.Println(LevelINFO, "P2PNetwork init ok")
|
gLog.Println(LevelINFO, "P2PNetwork init ok")
|
||||||
break
|
break
|
||||||
|
|||||||
32
protocol.go
32
protocol.go
@@ -6,6 +6,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -250,11 +252,12 @@ type RelayHeartbeat struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ReportBasic struct {
|
type ReportBasic struct {
|
||||||
OS string `json:"os,omitempty"`
|
OS string `json:"os,omitempty"`
|
||||||
Mac string `json:"mac,omitempty"`
|
Mac string `json:"mac,omitempty"`
|
||||||
LanIP string `json:"lanIP,omitempty"`
|
LanIP string `json:"lanIP,omitempty"`
|
||||||
IPv6 string `json:"IPv6,omitempty"`
|
IPv6 string `json:"IPv6,omitempty"`
|
||||||
Version string `json:"version,omitempty"`
|
Version string `json:"version,omitempty"`
|
||||||
|
NetInfo NetInfo `json:"netInfo,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReportConnect struct {
|
type ReportConnect struct {
|
||||||
@@ -278,3 +281,22 @@ type UpdateInfo struct {
|
|||||||
ErrorDetail string `json:"errorDetail,omitempty"`
|
ErrorDetail string `json:"errorDetail,omitempty"`
|
||||||
Url string `json:"url,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