mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-05-07 13:52:14 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b667e5b766 | ||
|
|
cd415e7bf4 |
16
README-ZH.md
16
README-ZH.md
@@ -101,6 +101,22 @@ cd到代码根目录,执行
|
||||
```
|
||||
make
|
||||
```
|
||||
手动编译特定系统和架构
|
||||
All GOOS values:
|
||||
```
|
||||
"aix", "android", "darwin", "dragonfly", "freebsd", "hurd", "illumos", "ios", "js", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos"
|
||||
```
|
||||
All GOARCH values:
|
||||
```
|
||||
"386", "amd64", "amd64p32", "arm", "arm64", "arm64be", "armbe", "loong64", "mips", "mips64", "mips64le", "mips64p32", "mips64p32le", "mipsle", "ppc", "ppc64", "ppc64le", "riscv", "riscv64", "s390", "s390x", "sparc", "sparc64", "wasm"
|
||||
```
|
||||
|
||||
比如linux+amd64
|
||||
```
|
||||
export GOPROXY=https://goproxy.io,direct
|
||||
go mod tidy
|
||||
CGO_ENABLED=0 env GOOS=linux GOARCH=amd64 go build -o openp2p --ldflags '-s -w ' -gcflags '-l' -p 8 -installsuffix cgo ./cmd
|
||||
```
|
||||
|
||||
## RoadMap
|
||||
近期计划:
|
||||
|
||||
17
README.md
17
README.md
@@ -109,6 +109,23 @@ cd root directory of the socure code and execute
|
||||
make
|
||||
```
|
||||
|
||||
build specified os and arch.
|
||||
All GOOS values:
|
||||
```
|
||||
"aix", "android", "darwin", "dragonfly", "freebsd", "hurd", "illumos", "ios", "js", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos"
|
||||
```
|
||||
All GOARCH values:
|
||||
```
|
||||
"386", "amd64", "amd64p32", "arm", "arm64", "arm64be", "armbe", "loong64", "mips", "mips64", "mips64le", "mips64p32", "mips64p32le", "mipsle", "ppc", "ppc64", "ppc64le", "riscv", "riscv64", "s390", "s390x", "sparc", "sparc64", "wasm"
|
||||
```
|
||||
|
||||
For example linux+amd64
|
||||
```
|
||||
export GOPROXY=https://goproxy.io,direct
|
||||
go mod tidy
|
||||
CGO_ENABLED=0 env GOOS=linux GOARCH=amd64 go build -o openp2p --ldflags '-s -w ' -gcflags '-l' -p 8 -installsuffix cgo ./cmd
|
||||
```
|
||||
|
||||
## RoadMap
|
||||
Short-Term:
|
||||
1. ~~Support IPv6.~~(100%)
|
||||
|
||||
@@ -58,9 +58,10 @@ func (c *Config) switchApp(app AppConfig, enabled int) {
|
||||
c.Apps[i].Enabled = enabled
|
||||
c.Apps[i].retryNum = 0
|
||||
c.Apps[i].nextRetryTime = time.Now()
|
||||
return
|
||||
break
|
||||
}
|
||||
}
|
||||
c.save()
|
||||
}
|
||||
func (c *Config) retryApp(peerNode string) {
|
||||
c.mtx.Lock()
|
||||
@@ -76,6 +77,7 @@ func (c *Config) retryApp(peerNode string) {
|
||||
func (c *Config) add(app AppConfig, override bool) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
if app.SrcPort == 0 || app.DstPort == 0 {
|
||||
gLog.Println(LvERROR, "invalid app ", app)
|
||||
return
|
||||
@@ -97,17 +99,19 @@ func (c *Config) delete(app AppConfig) {
|
||||
}
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
for i := 0; i < len(c.Apps); i++ {
|
||||
if c.Apps[i].Protocol == app.Protocol && c.Apps[i].SrcPort == app.SrcPort {
|
||||
c.Apps = append(c.Apps[:i], c.Apps[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *Config) save() {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
// c.mtx.Lock()
|
||||
// defer c.mtx.Unlock() // internal call
|
||||
data, _ := json.MarshalIndent(c, "", " ")
|
||||
err := ioutil.WriteFile("config.json", data, 0644)
|
||||
if err != nil {
|
||||
@@ -142,6 +146,7 @@ func (c *Config) load() error {
|
||||
func (c *Config) setToken(token uint64) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
if token != 0 {
|
||||
c.Network.Token = token
|
||||
}
|
||||
@@ -149,16 +154,19 @@ func (c *Config) setToken(token uint64) {
|
||||
func (c *Config) setUser(user string) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
c.Network.User = user
|
||||
}
|
||||
func (c *Config) setNode(node string) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
c.Network.Node = node
|
||||
}
|
||||
func (c *Config) setShareBandwidth(bw int) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
defer c.save()
|
||||
c.Network.ShareBandwidth = bw
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ var (
|
||||
ErrorLogin = errors.New("user or password not correct")
|
||||
ErrNodeTooShort = errors.New("node name too short, it must >=8 charaters")
|
||||
ErrPeerOffline = errors.New("peer offline")
|
||||
ErrNetwork = errors.New("network error")
|
||||
ErrMsgFormat = errors.New("message format wrong")
|
||||
ErrVersionNotCompatible = errors.New("version not compatible")
|
||||
ErrOverlayConnDisconnect = errors.New("overlay connection is disconnected")
|
||||
|
||||
@@ -176,7 +176,7 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
}
|
||||
pn.write(MsgReport, MsgReportApps, &req)
|
||||
case MsgPushReportLog:
|
||||
gLog.Println(LvINFO, "MsgPushReportLog")
|
||||
gLog.Println(LvDEBUG, "MsgPushReportLog")
|
||||
req := ReportLogReq{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &req)
|
||||
if err != nil {
|
||||
@@ -241,7 +241,6 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
newConf.Protocol = newApp.Protocol
|
||||
newConf.SrcPort = newApp.SrcPort
|
||||
gConf.add(newConf, false)
|
||||
gConf.save() // save quickly for the next request reportApplist
|
||||
pn.DeleteApp(oldConf) // DeleteApp may cost some times, execute at the end
|
||||
// autoReconnect will auto AddApp
|
||||
// pn.AddApp(config)
|
||||
@@ -256,7 +255,6 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
}
|
||||
gConf.setNode(req.NewName)
|
||||
gConf.setShareBandwidth(req.Bandwidth)
|
||||
gConf.save()
|
||||
// TODO: hot reload
|
||||
os.Exit(0)
|
||||
case MsgPushSwitchApp:
|
||||
|
||||
@@ -23,11 +23,11 @@ func handshakeC2C(t *P2PTunnel) (err error) {
|
||||
gLog.Println(LvDEBUG, "handshakeC2C write MsgPunchHandshake error:", err)
|
||||
return err
|
||||
}
|
||||
ra, head, _, _, err := UDPRead(conn, 5000)
|
||||
ra, head, _, _, err := UDPRead(conn, SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
gLog.Println(LvDEBUG, err, ", return this error when ip was not reachable, retry read")
|
||||
ra, head, _, _, err = UDPRead(conn, 5000)
|
||||
ra, head, _, _, err = UDPRead(conn, SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
gLog.Println(LvDEBUG, "handshakeC2C read MsgPunchHandshake error:", err)
|
||||
return err
|
||||
@@ -38,7 +38,7 @@ func handshakeC2C(t *P2PTunnel) (err error) {
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshake {
|
||||
gLog.Printf(LvDEBUG, "read %d handshake ", t.id)
|
||||
UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
_, head, _, _, err = UDPRead(conn, 5000)
|
||||
_, head, _, _, err = UDPRead(conn, SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
gLog.Println(LvDEBUG, "handshakeC2C write MsgPunchHandshakeAck error", err)
|
||||
return err
|
||||
@@ -140,7 +140,7 @@ func handshakeS2C(t *P2PTunnel) error {
|
||||
}
|
||||
defer conn.Close()
|
||||
UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshake, P2PHandshakeReq{ID: t.id})
|
||||
_, head, _, _, err := UDPRead(conn, 10000)
|
||||
_, head, _, _, err := UDPRead(conn, SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
// gLog.Println(LevelDEBUG, "one of the handshake error:", err)
|
||||
return err
|
||||
@@ -155,7 +155,7 @@ func handshakeS2C(t *P2PTunnel) error {
|
||||
if head.MainType == MsgP2P && head.SubType == MsgPunchHandshake {
|
||||
gLog.Printf(LvDEBUG, "handshakeS2C read %d handshake ", t.id)
|
||||
UDPWrite(conn, t.ra, MsgP2P, MsgPunchHandshakeAck, P2PHandshakeReq{ID: t.id})
|
||||
_, head, _, _, err = UDPRead(conn, 5000)
|
||||
_, head, _, _, err = UDPRead(conn, SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
gLog.Println(LvDEBUG, "handshakeS2C handshake error")
|
||||
return err
|
||||
|
||||
@@ -34,7 +34,7 @@ func natTCP(serverHost string, serverPort int, localPort int) (publicIP string,
|
||||
return
|
||||
}
|
||||
b := make([]byte, 1000)
|
||||
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
|
||||
conn.SetReadDeadline(time.Now().Add(NatTestTimeout))
|
||||
n, rderr := conn.Read(b)
|
||||
if rderr != nil {
|
||||
fmt.Printf("Read error: %s\n", rderr)
|
||||
@@ -153,12 +153,12 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP
|
||||
}
|
||||
log.Println("PublicIP:", ext)
|
||||
|
||||
externalPort, err := nat.AddPortMapping("udp", echoPort, echoPort, "openp2p", 30)
|
||||
externalPort, err := nat.AddPortMapping("udp", echoPort, echoPort, "openp2p", 604800) // 7 days, upnp will perform failed when os start
|
||||
if err != nil {
|
||||
gLog.Println(LvDEBUG, "could not add udp UPNP port mapping", externalPort)
|
||||
break
|
||||
} else {
|
||||
nat.AddPortMapping("tcp", echoPort, echoPort, "openp2p", 604800)
|
||||
nat.AddPortMapping("tcp", echoPort, echoPort, "openp2p", 604800) // 7 days
|
||||
}
|
||||
}
|
||||
gLog.Printf(LvDEBUG, "public ip test start %s:%d", publicIP, echoPort)
|
||||
|
||||
@@ -42,7 +42,6 @@ type overlayConn struct {
|
||||
func (oConn *overlayConn) run() {
|
||||
gLog.Printf(LvDEBUG, "%d overlayConn run start", oConn.id)
|
||||
defer gLog.Printf(LvDEBUG, "%d overlayConn run end", oConn.id)
|
||||
oConn.running = true
|
||||
oConn.lastReadUDPTs = time.Now()
|
||||
buffer := make([]byte, ReadBuffLen+PaddingSize) // 16 bytes for padding
|
||||
reuseBuff := buffer[:ReadBuffLen]
|
||||
@@ -117,7 +116,7 @@ func (oConn *overlayConn) Read(reuseBuff []byte) (buff []byte, dataLen int, err
|
||||
err = ErrDeadlineExceeded
|
||||
}
|
||||
} else { // as client
|
||||
oConn.connUDP.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||
oConn.connUDP.SetReadDeadline(time.Now().Add(UDPReadTimeout))
|
||||
dataLen, _, err = oConn.connUDP.ReadFrom(reuseBuff)
|
||||
if err == nil {
|
||||
oConn.lastReadUDPTs = time.Now()
|
||||
@@ -127,7 +126,7 @@ func (oConn *overlayConn) Read(reuseBuff []byte) (buff []byte, dataLen int, err
|
||||
return
|
||||
}
|
||||
if oConn.connTCP != nil {
|
||||
oConn.connTCP.SetReadDeadline(time.Now().Add(time.Second * 5))
|
||||
oConn.connTCP.SetReadDeadline(time.Now().Add(UDPReadTimeout))
|
||||
dataLen, err = oConn.connTCP.Read(reuseBuff)
|
||||
buff = reuseBuff
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ func (app *p2pApp) listenTCP() error {
|
||||
rtid: app.rtid,
|
||||
appID: app.id,
|
||||
appKey: app.key,
|
||||
running: true,
|
||||
}
|
||||
// pre-calc key bytes for encrypt
|
||||
if oConn.appKey != 0 {
|
||||
@@ -100,6 +101,8 @@ func (app *p2pApp) listenTCP() error {
|
||||
msgWithHead := append(relayHead.Bytes(), msg...)
|
||||
app.tunnel.conn.WriteBytes(MsgP2P, MsgRelayData, msgWithHead)
|
||||
}
|
||||
// TODO: wait OverlayConnectRsp instead of sleep
|
||||
time.Sleep(time.Second) // waiting remote node connection ok
|
||||
go oConn.run()
|
||||
}
|
||||
return nil
|
||||
@@ -117,7 +120,7 @@ func (app *p2pApp) listenUDP() error {
|
||||
buffer := make([]byte, 64*1024+PaddingSize)
|
||||
udpID := make([]byte, 8)
|
||||
for {
|
||||
app.listenerUDP.SetReadDeadline(time.Now().Add(time.Second * 10))
|
||||
app.listenerUDP.SetReadDeadline(time.Now().Add(UDPReadTimeout))
|
||||
len, remoteAddr, err := app.listenerUDP.ReadFrom(buffer)
|
||||
if err != nil {
|
||||
if ne, ok := err.(net.Error); ok && ne.Timeout() {
|
||||
@@ -152,6 +155,7 @@ func (app *p2pApp) listenUDP() error {
|
||||
rtid: app.rtid,
|
||||
appID: app.id,
|
||||
appKey: app.key,
|
||||
running: true,
|
||||
}
|
||||
// calc key bytes for encrypt
|
||||
if oConn.appKey != 0 {
|
||||
@@ -180,6 +184,8 @@ func (app *p2pApp) listenUDP() error {
|
||||
msgWithHead := append(relayHead.Bytes(), msg...)
|
||||
app.tunnel.conn.WriteBytes(MsgP2P, MsgRelayData, msgWithHead)
|
||||
}
|
||||
// TODO: wait OverlayConnectRsp instead of sleep
|
||||
time.Sleep(time.Second) // waiting remote node connection ok
|
||||
go oConn.run()
|
||||
oConn.udpData <- dupData.Bytes()
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (pn *P2PNetwork) run() {
|
||||
case <-pn.restartCh:
|
||||
pn.online = false
|
||||
pn.wgReconnect.Wait() // wait read/autorunapp goroutine end
|
||||
time.Sleep(NatTestTimeout)
|
||||
time.Sleep(ClientAPITimeout)
|
||||
err := pn.init()
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "P2PNetwork init error:", err)
|
||||
@@ -128,12 +128,13 @@ func (pn *P2PNetwork) runAll() {
|
||||
continue
|
||||
}
|
||||
|
||||
if time.Now().Add(-time.Minute * 15).After(config.retryTime) { // run normally 15min, reset retrynum
|
||||
config.retryNum = 0
|
||||
if config.retryNum > 0 { // first time not show reconnect log
|
||||
gLog.Printf(LvINFO, "detect app %s disconnect, reconnecting the %d times...", config.AppName, config.retryNum)
|
||||
if time.Now().Add(-time.Minute * 15).After(config.retryTime) { // run normally 15min, reset retrynum
|
||||
config.retryNum = 0
|
||||
}
|
||||
}
|
||||
|
||||
config.retryNum++
|
||||
gLog.Printf(LvINFO, "detect app %s disconnect, reconnecting the %d times...", config.AppName, config.retryNum)
|
||||
config.retryTime = time.Now()
|
||||
config.nextRetryTime = time.Now().Add(retryInterval)
|
||||
config.connectTime = time.Now()
|
||||
@@ -166,7 +167,7 @@ func (pn *P2PNetwork) addRelayTunnel(config AppConfig) (*P2PTunnel, uint64, stri
|
||||
defer gLog.Printf(LvINFO, "addRelayTunnel to %s end", config.PeerNode)
|
||||
// request a relay node or specify manually(TODO)
|
||||
pn.write(MsgRelay, MsgRelayNodeReq, &RelayNodeReq{config.PeerNode})
|
||||
head, body := pn.read("", MsgRelay, MsgRelayNodeRsp, time.Second*10)
|
||||
head, body := pn.read("", MsgRelay, MsgRelayNodeRsp, ClientAPITimeout)
|
||||
if head == nil {
|
||||
return nil, 0, "", errors.New("read MsgRelayNodeRsp error")
|
||||
}
|
||||
@@ -407,6 +408,7 @@ func (pn *P2PNetwork) addDirectTunnel(config AppConfig, tid uint64) (*P2PTunnel,
|
||||
t.config.linkMode = LinkModeTCPPunch
|
||||
t.config.isUnderlayServer = 0
|
||||
if err = pn.newTunnel(t, tid, isClient); err == nil {
|
||||
gLog.Println(LvINFO, "TCP4 Punch ok")
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
@@ -468,7 +470,7 @@ func (pn *P2PNetwork) init() error {
|
||||
}
|
||||
gLog.Println(LvDEBUG, "detect NAT type:", pn.config.natType, " publicIP:", pn.config.publicIP)
|
||||
gatewayURL := fmt.Sprintf("%s:%d", pn.config.ServerHost, pn.config.ServerPort)
|
||||
uri := "/openp2p/v1/login"
|
||||
uri := "/api/v1/login"
|
||||
config := tls.Config{InsecureSkipVerify: true} // let's encrypt root cert "DST Root CA X3" expired at 2021/09/29. many old system(windows server 2008 etc) will not trust our cert
|
||||
websocket.DefaultDialer.TLSClientConfig = &config
|
||||
u := url.URL{Scheme: "wss", Host: gatewayURL, Path: uri}
|
||||
@@ -560,7 +562,6 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) {
|
||||
gConf.setNode(rsp.Node)
|
||||
pn.config.Node = rsp.Node
|
||||
}
|
||||
gConf.save()
|
||||
pn.localTs = time.Now().Unix()
|
||||
gLog.Printf(LvINFO, "login ok. user=%s,node=%s,Server ts=%d, local ts=%d", rsp.User, rsp.Node, rsp.Ts, pn.localTs)
|
||||
}
|
||||
@@ -608,6 +609,7 @@ func (pn *P2PNetwork) write(mainType uint16, subType uint16, packet interface{})
|
||||
defer pn.writeMtx.Unlock()
|
||||
if err = pn.conn.WriteMessage(websocket.BinaryMessage, msg); err != nil {
|
||||
gLog.Printf(LvERROR, "write msgType %d,%d error:%s", mainType, subType, err)
|
||||
pn.conn.Close()
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -650,6 +652,7 @@ func (pn *P2PNetwork) push(to string, subType uint16, packet interface{}) error
|
||||
defer pn.writeMtx.Unlock()
|
||||
if err = pn.conn.WriteMessage(websocket.BinaryMessage, pushMsg); err != nil {
|
||||
gLog.Printf(LvERROR, "push to %s error:%s", to, err)
|
||||
pn.conn.Close()
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -700,6 +703,7 @@ func (pn *P2PNetwork) updateAppHeartbeat(appID uint64) {
|
||||
})
|
||||
}
|
||||
|
||||
// ipv6 will expired need to refresh.
|
||||
func (pn *P2PNetwork) refreshIPv6(force bool) {
|
||||
if !force && !IsIPv6(pn.config.publicIPv6) { // not support ipv6, not refresh
|
||||
return
|
||||
|
||||
@@ -36,7 +36,7 @@ func (t *P2PTunnel) requestPeerInfo() error {
|
||||
t.pn.write(MsgQuery, MsgQueryPeerInfoReq, &QueryPeerInfoReq{t.config.peerToken, t.config.PeerNode})
|
||||
head, body := t.pn.read("", MsgQuery, MsgQueryPeerInfoRsp, time.Second*10)
|
||||
if head == nil {
|
||||
return ErrPeerOffline
|
||||
return ErrNetwork // network error, should not be ErrPeerOffline
|
||||
}
|
||||
rsp := QueryPeerInfoRsp{}
|
||||
err := json.Unmarshal(body, &rsp)
|
||||
@@ -444,7 +444,7 @@ func (t *P2PTunnel) readLoop() {
|
||||
continue
|
||||
}
|
||||
overlayID := binary.LittleEndian.Uint64(body[:8])
|
||||
gLog.Printf(LvDEBUG, "%d tunnel read overlay data %d", t.id, overlayID)
|
||||
gLog.Printf(LvDEBUG, "%d tunnel read overlay data %d bodylen=%d", t.id, overlayID, head.DataLen)
|
||||
s, ok := t.overlayConns.Load(overlayID)
|
||||
if !ok {
|
||||
// debug level, when overlay connection closed, always has some packet not found tunnel
|
||||
@@ -515,6 +515,7 @@ func (t *P2PTunnel) readLoop() {
|
||||
rtid: req.RelayTunnelID,
|
||||
appID: req.AppID,
|
||||
appKey: GetKey(req.AppID),
|
||||
running: true,
|
||||
}
|
||||
if req.Protocol == "udp" {
|
||||
oConn.connUDP, err = net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP(req.DstIP), Port: req.DstPort})
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const OpenP2PVersion = "3.6.8"
|
||||
const OpenP2PVersion = "3.8.0"
|
||||
const ProductName string = "openp2p"
|
||||
const LeastSupportVersion = "3.0.0"
|
||||
|
||||
@@ -139,7 +139,7 @@ const (
|
||||
SymmetricHandshakeNum = 800 // 0.992379
|
||||
// SymmetricHandshakeNum = 1000 // 0.999510
|
||||
SymmetricHandshakeInterval = time.Millisecond
|
||||
SymmetricHandshakeAckTimeout = time.Second * 11
|
||||
SymmetricHandshakeAckTimeout = time.Second * 5
|
||||
PeerAddRelayTimeount = time.Second * 20
|
||||
CheckActiveTimeout = time.Second * 5
|
||||
PaddingSize = 16
|
||||
@@ -148,6 +148,7 @@ const (
|
||||
RetryInterval = time.Second * 30
|
||||
PublicIPEchoTimeout = time.Second * 1
|
||||
NatTestTimeout = time.Second * 5
|
||||
UDPReadTimeout = time.Second * 5
|
||||
ClientAPITimeout = time.Second * 10
|
||||
MaxDirectTry = 3
|
||||
)
|
||||
|
||||
@@ -18,9 +18,9 @@ func UDPWrite(conn *net.UDPConn, dst net.Addr, mainType uint16, subType uint16,
|
||||
return conn.WriteTo(msg, dst)
|
||||
}
|
||||
|
||||
func UDPRead(conn *net.UDPConn, timeout int) (ra net.Addr, head *openP2PHeader, result []byte, len int, err error) {
|
||||
func UDPRead(conn *net.UDPConn, timeout time.Duration) (ra net.Addr, head *openP2PHeader, result []byte, len int, err error) {
|
||||
if timeout > 0 {
|
||||
deadline := time.Now().Add(time.Millisecond * time.Duration(timeout))
|
||||
deadline := time.Now().Add(timeout)
|
||||
err = conn.SetReadDeadline(deadline)
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "SetReadDeadline error")
|
||||
|
||||
@@ -69,7 +69,7 @@ func (conn *underlayTCP) Close() error {
|
||||
|
||||
func listenTCP(host string, port int, localPort int, mode string) (*underlayTCP, error) {
|
||||
if mode == LinkModeTCPPunch {
|
||||
c, err := reuse.DialTimeout("tcp", fmt.Sprintf("0.0.0.0:%d", localPort), fmt.Sprintf("%s:%d", host, port), SymmetricHandshakeAckTimeout) // TODO: timeout
|
||||
c, err := reuse.DialTimeout("tcp", fmt.Sprintf("0.0.0.0:%d", localPort), fmt.Sprintf("%s:%d", host, port), SymmetricHandshakeAckTimeout)
|
||||
if err != nil {
|
||||
gLog.Println(LvDEBUG, "send tcp punch: ", err)
|
||||
return nil, err
|
||||
|
||||
69
core/util_freebsd.go
Normal file
69
core/util_freebsd.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package openp2p
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultInstallPath = "/usr/local/openp2p"
|
||||
defaultBinName = "openp2p"
|
||||
)
|
||||
|
||||
func getOsName() (osName string) {
|
||||
var sysnamePath string
|
||||
sysnamePath = "/etc/redhat-release"
|
||||
_, err := os.Stat(sysnamePath)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
str := "PRETTY_NAME="
|
||||
f, err := os.Open("/etc/os-release")
|
||||
if err == nil {
|
||||
buf := bufio.NewReader(f)
|
||||
for {
|
||||
line, err := buf.ReadString('\n')
|
||||
if err == nil {
|
||||
line = strings.TrimSpace(line)
|
||||
pos := strings.Count(line, str)
|
||||
if pos > 0 {
|
||||
len1 := len([]rune(str)) + 1
|
||||
rs := []rune(line)
|
||||
osName = string(rs[len1 : (len(rs))-1])
|
||||
break
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buff, err := ioutil.ReadFile(sysnamePath)
|
||||
if err == nil {
|
||||
osName = string(bytes.TrimSpace(buff))
|
||||
}
|
||||
}
|
||||
if osName == "" {
|
||||
osName = "FreeBSD"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func setRLimit() error {
|
||||
var limit syscall.Rlimit
|
||||
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
||||
return err
|
||||
}
|
||||
limit.Max = 65536
|
||||
limit.Cur = limit.Max
|
||||
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setFirewall() {
|
||||
}
|
||||
Reference in New Issue
Block a user