Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b8d3f7d47 |
+6
-2
@@ -39,7 +39,12 @@ P2P直连可以让你的设备跑满带宽。不论你的设备在任何网络
|
|||||||
分别在本地和远程电脑下载后双击运行,一键安装
|
分别在本地和远程电脑下载后双击运行,一键安装
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
Windows默认会阻止没有花钱买它家证书签名过的程序,选择“仍要运行”即可。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
### 3.新建P2P应用
|
### 3.新建P2P应用
|
||||||
|
|
||||||

|

|
||||||
@@ -120,8 +125,7 @@ go build
|
|||||||
|
|
||||||
## 参与贡献
|
## 参与贡献
|
||||||
TODO或ISSUE里如果有你擅长的领域,或者你有特别好的主意,可以加入OpenP2P项目,贡献你的代码。待项目茁壮成长后,你们就是知名开源项目的主要代码贡献者,岂不快哉。
|
TODO或ISSUE里如果有你擅长的领域,或者你有特别好的主意,可以加入OpenP2P项目,贡献你的代码。待项目茁壮成长后,你们就是知名开源项目的主要代码贡献者,岂不快哉。
|
||||||
## 商业合作
|
|
||||||
它是一个中国人发起的项目,更懂国内网络环境,更懂用户需求,更好的企业级支持
|
|
||||||
## 技术交流
|
## 技术交流
|
||||||
QQ群:16947733
|
QQ群:16947733
|
||||||
邮箱:openp2p.cn@gmail.com tenderiron@139.com
|
邮箱:openp2p.cn@gmail.com tenderiron@139.com
|
||||||
|
|||||||
@@ -43,6 +43,12 @@ Download on local and remote computers and double-click to run, one-click instal
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
By default, Windows will block programs that have not been signed by the Microsoft's certificate, and you can select "Run anyway".
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
### 3.New P2PApp
|
### 3.New P2PApp
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -32,15 +31,17 @@ type AppConfig struct {
|
|||||||
retryTime time.Time
|
retryTime time.Time
|
||||||
nextRetryTime time.Time
|
nextRetryTime time.Time
|
||||||
shareBandwidth int
|
shareBandwidth int
|
||||||
|
errMsg string
|
||||||
|
connectTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add loglevel, maxlogfilesize
|
// TODO: add loglevel, maxlogfilesize
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Network NetworkConfig `json:"network"`
|
Network NetworkConfig `json:"network"`
|
||||||
Apps []*AppConfig `json:"apps"`
|
Apps []*AppConfig `json:"apps"`
|
||||||
LogLevel int
|
LogLevel int
|
||||||
|
daemonMode bool
|
||||||
mtx sync.Mutex
|
mtx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) switchApp(app AppConfig, enabled int) {
|
func (c *Config) switchApp(app AppConfig, enabled int) {
|
||||||
@@ -115,6 +116,27 @@ func (c *Config) load() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) setToken(token uint64) {
|
||||||
|
c.mtx.Lock()
|
||||||
|
defer c.mtx.Unlock()
|
||||||
|
c.Network.Token = token
|
||||||
|
}
|
||||||
|
func (c *Config) setUser(user string) {
|
||||||
|
c.mtx.Lock()
|
||||||
|
defer c.mtx.Unlock()
|
||||||
|
c.Network.User = user
|
||||||
|
}
|
||||||
|
func (c *Config) setNode(node string) {
|
||||||
|
c.mtx.Lock()
|
||||||
|
defer c.mtx.Unlock()
|
||||||
|
c.Network.Node = node
|
||||||
|
}
|
||||||
|
func (c *Config) setShareBandwidth(bw int) {
|
||||||
|
c.mtx.Lock()
|
||||||
|
defer c.mtx.Unlock()
|
||||||
|
c.Network.ShareBandwidth = bw
|
||||||
|
}
|
||||||
|
|
||||||
type NetworkConfig struct {
|
type NetworkConfig struct {
|
||||||
// local info
|
// local info
|
||||||
Token uint64
|
Token uint64
|
||||||
@@ -147,6 +169,7 @@ func parseParams() {
|
|||||||
appName := flag.String("appname", "", "app name")
|
appName := flag.String("appname", "", "app name")
|
||||||
shareBandwidth := flag.Int("sharebandwidth", 10, "N mbps share bandwidth limit, private network no limit")
|
shareBandwidth := flag.Int("sharebandwidth", 10, "N mbps share bandwidth limit, private network no limit")
|
||||||
daemonMode := flag.Bool("d", false, "daemonMode")
|
daemonMode := flag.Bool("d", false, "daemonMode")
|
||||||
|
notVerbose := flag.Bool("nv", false, "not log console")
|
||||||
logLevel := flag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error")
|
logLevel := flag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -161,8 +184,8 @@ func parseParams() {
|
|||||||
if config.SrcPort != 0 {
|
if config.SrcPort != 0 {
|
||||||
gConf.add(config, true)
|
gConf.add(config, true)
|
||||||
}
|
}
|
||||||
gConf.mtx.Lock()
|
// gConf.mtx.Lock() // when calling this func it's single-thread no lock
|
||||||
|
gConf.daemonMode = *daemonMode
|
||||||
// spec paramters in commandline will always be used
|
// spec paramters in commandline will always be used
|
||||||
flag.Visit(func(f *flag.Flag) {
|
flag.Visit(func(f *flag.Flag) {
|
||||||
if f.Name == "sharebandwidth" {
|
if f.Name == "sharebandwidth" {
|
||||||
@@ -203,11 +226,9 @@ func parseParams() {
|
|||||||
gConf.Network.UDPPort1 = 27182
|
gConf.Network.UDPPort1 = 27182
|
||||||
gConf.Network.UDPPort2 = 27183
|
gConf.Network.UDPPort2 = 27183
|
||||||
gLog.setLevel(LogLevel(gConf.LogLevel))
|
gLog.setLevel(LogLevel(gConf.LogLevel))
|
||||||
gConf.mtx.Unlock()
|
if *notVerbose {
|
||||||
gConf.save()
|
gLog.setMode(LogFile)
|
||||||
if *daemonMode {
|
|
||||||
d := daemon{}
|
|
||||||
d.run()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
// gConf.mtx.Unlock()
|
||||||
|
gConf.save()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,10 +64,18 @@ func (d *daemon) run() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
args = append(args, "-nv")
|
||||||
for {
|
for {
|
||||||
// start worker
|
// start worker
|
||||||
|
tmpDump := filepath.Join("log", "dump.log.tmp")
|
||||||
|
dumpFile := filepath.Join("log", "dump.log")
|
||||||
|
f, err := os.Create(filepath.Join(tmpDump))
|
||||||
|
if err != nil {
|
||||||
|
gLog.Printf(LevelERROR, "start worker error:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
gLog.Println(LevelINFO, "start worker process, args:", args)
|
gLog.Println(LevelINFO, "start worker process, args:", args)
|
||||||
execSpec := &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}}
|
execSpec := &os.ProcAttr{Env: append(os.Environ(), "GOTRACEBACK=crash"), Files: []*os.File{os.Stdin, os.Stdout, f}}
|
||||||
p, err := os.StartProcess(binPath, args, execSpec)
|
p, err := os.StartProcess(binPath, args, execSpec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gLog.Printf(LevelERROR, "start worker error:%s", err)
|
gLog.Printf(LevelERROR, "start worker error:%s", err)
|
||||||
@@ -75,6 +83,12 @@ func (d *daemon) run() {
|
|||||||
}
|
}
|
||||||
d.proc = p
|
d.proc = p
|
||||||
_, _ = p.Wait()
|
_, _ = p.Wait()
|
||||||
|
f.Close()
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
err = os.Rename(tmpDump, dumpFile)
|
||||||
|
if err != nil {
|
||||||
|
gLog.Printf(LevelERROR, "rename dump error:%s", err)
|
||||||
|
}
|
||||||
if !d.running {
|
if !d.running {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -127,6 +127,7 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
|||||||
}
|
}
|
||||||
appInfo := AppInfo{
|
appInfo := AppInfo{
|
||||||
AppName: config.AppName,
|
AppName: config.AppName,
|
||||||
|
Error: config.errMsg,
|
||||||
Protocol: config.Protocol,
|
Protocol: config.Protocol,
|
||||||
SrcPort: config.SrcPort,
|
SrcPort: config.SrcPort,
|
||||||
RelayNode: relayNode,
|
RelayNode: relayNode,
|
||||||
@@ -137,7 +138,8 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
|||||||
PeerUser: config.PeerUser,
|
PeerUser: config.PeerUser,
|
||||||
PeerIP: config.peerIP,
|
PeerIP: config.peerIP,
|
||||||
PeerNatType: config.peerNatType,
|
PeerNatType: config.peerNatType,
|
||||||
RetryTime: config.retryTime.String(),
|
RetryTime: config.retryTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||||
|
ConnectTime: config.connectTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||||
IsActive: appActive,
|
IsActive: appActive,
|
||||||
Enabled: config.Enabled,
|
Enabled: config.Enabled,
|
||||||
}
|
}
|
||||||
@@ -180,10 +182,8 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
|||||||
gLog.Printf(LevelERROR, "wrong MsgPushEditNode:%s %s", err, string(msg[openP2PHeaderSize:]))
|
gLog.Printf(LevelERROR, "wrong MsgPushEditNode:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gConf.mtx.Lock()
|
gConf.setNode(req.NewName)
|
||||||
gConf.Network.Node = req.NewName
|
gConf.setShareBandwidth(req.Bandwidth)
|
||||||
gConf.Network.ShareBandwidth = req.Bandwidth
|
|
||||||
gConf.mtx.Unlock()
|
|
||||||
gConf.save()
|
gConf.save()
|
||||||
// TODO: hot reload
|
// TODO: hot reload
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|||||||
@@ -97,6 +97,11 @@ func (vl *V8log) setLevel(level LogLevel) {
|
|||||||
defer vl.mtx.Unlock()
|
defer vl.mtx.Unlock()
|
||||||
vl.level = level
|
vl.level = level
|
||||||
}
|
}
|
||||||
|
func (vl *V8log) setMode(mode int) {
|
||||||
|
vl.mtx.Lock()
|
||||||
|
defer vl.mtx.Unlock()
|
||||||
|
vl.mode = mode
|
||||||
|
}
|
||||||
|
|
||||||
func (vl *V8log) checkFile() {
|
func (vl *V8log) checkFile() {
|
||||||
if vl.maxLogSize <= 0 {
|
if vl.maxLogSize <= 0 {
|
||||||
|
|||||||
+8
-1
@@ -40,9 +40,16 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
installByFilename()
|
installByFilename()
|
||||||
}
|
}
|
||||||
|
parseParams()
|
||||||
gLog.Println(LevelINFO, "openp2p start. version: ", OpenP2PVersion)
|
gLog.Println(LevelINFO, "openp2p start. version: ", OpenP2PVersion)
|
||||||
gLog.Println(LevelINFO, "Contact: QQ Group: 16947733, Email: [email protected]")
|
gLog.Println(LevelINFO, "Contact: QQ Group: 16947733, Email: [email protected]")
|
||||||
parseParams()
|
|
||||||
|
if gConf.daemonMode {
|
||||||
|
d := daemon{}
|
||||||
|
d.run()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
gLog.Println(LevelINFO, &gConf)
|
gLog.Println(LevelINFO, &gConf)
|
||||||
setFirewall()
|
setFirewall()
|
||||||
network := P2PNetworkInstance(&gConf.Network)
|
network := P2PNetworkInstance(&gConf.Network)
|
||||||
|
|||||||
+14
-7
@@ -94,9 +94,11 @@ func (pn *P2PNetwork) Connect(timeout int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pn *P2PNetwork) runAll() {
|
func (pn *P2PNetwork) runAll() {
|
||||||
gConf.mtx.Lock()
|
gConf.mtx.Lock() // lock for copy gConf.Apps and the modification of config(it's pointer)
|
||||||
defer gConf.mtx.Unlock()
|
defer gConf.mtx.Unlock()
|
||||||
for _, config := range gConf.Apps {
|
allApps := gConf.Apps // read a copy, other thread will modify the gConf.Apps
|
||||||
|
|
||||||
|
for _, config := range allApps {
|
||||||
if config.nextRetryTime.After(time.Now()) {
|
if config.nextRetryTime.After(time.Now()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -133,7 +135,13 @@ func (pn *P2PNetwork) runAll() {
|
|||||||
increase = 900
|
increase = 900
|
||||||
}
|
}
|
||||||
config.nextRetryTime = time.Now().Add(time.Second * time.Duration(increase)) // exponential increase retry time. 1.3^x
|
config.nextRetryTime = time.Now().Add(time.Second * time.Duration(increase)) // exponential increase retry time. 1.3^x
|
||||||
pn.AddApp(*config)
|
config.connectTime = time.Now()
|
||||||
|
gConf.mtx.Unlock() // AddApp will take a period of time
|
||||||
|
err := pn.AddApp(*config)
|
||||||
|
gConf.mtx.Lock()
|
||||||
|
if err != nil {
|
||||||
|
config.errMsg = err.Error()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (pn *P2PNetwork) autorunApp() {
|
func (pn *P2PNetwork) autorunApp() {
|
||||||
@@ -202,6 +210,7 @@ func (pn *P2PNetwork) addRelayTunnel(config AppConfig, appid uint64, appkey uint
|
|||||||
return t, rspID.ID, rsp.Mode, err
|
return t, rspID.ID, rsp.Mode, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use *AppConfig to save status
|
||||||
func (pn *P2PNetwork) AddApp(config AppConfig) error {
|
func (pn *P2PNetwork) AddApp(config AppConfig) error {
|
||||||
gLog.Printf(LevelINFO, "addApp %s to %s:%s:%d start", config.AppName, config.PeerNode, config.DstHost, config.DstPort)
|
gLog.Printf(LevelINFO, "addApp %s to %s:%s:%d start", config.AppName, config.PeerNode, config.DstHost, config.DstPort)
|
||||||
defer gLog.Printf(LevelINFO, "addApp %s to %s:%s:%d end", config.AppName, config.PeerNode, config.DstHost, config.DstPort)
|
defer gLog.Printf(LevelINFO, "addApp %s to %s:%s:%d end", config.AppName, config.PeerNode, config.DstHost, config.DstPort)
|
||||||
@@ -459,10 +468,8 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) {
|
|||||||
pn.serverTs = rsp.Ts
|
pn.serverTs = rsp.Ts
|
||||||
pn.config.Token = rsp.Token
|
pn.config.Token = rsp.Token
|
||||||
pn.config.User = rsp.User
|
pn.config.User = rsp.User
|
||||||
gConf.mtx.Lock()
|
gConf.setToken(rsp.Token)
|
||||||
gConf.Network.Token = rsp.Token
|
gConf.setUser(rsp.User)
|
||||||
gConf.Network.User = rsp.User
|
|
||||||
gConf.mtx.Unlock()
|
|
||||||
gConf.save()
|
gConf.save()
|
||||||
pn.localTs = time.Now().Unix()
|
pn.localTs = time.Now().Unix()
|
||||||
gLog.Printf(LevelINFO, "login ok. user=%s,Server ts=%d, local ts=%d", rsp.User, rsp.Ts, pn.localTs)
|
gLog.Printf(LevelINFO, "login ok. user=%s,Server ts=%d, local ts=%d", rsp.User, rsp.Ts, pn.localTs)
|
||||||
|
|||||||
+4
-2
@@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const OpenP2PVersion = "1.3.0"
|
const OpenP2PVersion = "1.4.2"
|
||||||
const ProducnName string = "openp2p"
|
const ProducnName string = "openp2p"
|
||||||
|
|
||||||
type openP2PHeader struct {
|
type openP2PHeader struct {
|
||||||
@@ -134,6 +134,7 @@ const (
|
|||||||
PublicIPEchoTimeout = time.Second * 3
|
PublicIPEchoTimeout = time.Second * 3
|
||||||
NatTestTimeout = time.Second * 10
|
NatTestTimeout = time.Second * 10
|
||||||
ClientAPITimeout = time.Second * 10
|
ClientAPITimeout = time.Second * 10
|
||||||
|
MaxDirectTry = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// NATNone has public ip
|
// NATNone has public ip
|
||||||
@@ -270,7 +271,7 @@ type ReportConnect struct {
|
|||||||
NatType int `json:"natType,omitempty"`
|
NatType int `json:"natType,omitempty"`
|
||||||
PeerNode string `json:"peerNode,omitempty"`
|
PeerNode string `json:"peerNode,omitempty"`
|
||||||
DstPort int `json:"dstPort,omitempty"`
|
DstPort int `json:"dstPort,omitempty"`
|
||||||
DstHost string `json:"dsdtHost,omitempty"`
|
DstHost string `json:"dstHost,omitempty"`
|
||||||
PeerUser string `json:"peerUser,omitempty"`
|
PeerUser string `json:"peerUser,omitempty"`
|
||||||
PeerNatType int `json:"peerNatType,omitempty"`
|
PeerNatType int `json:"peerNatType,omitempty"`
|
||||||
PeerIP string `json:"peerIP,omitempty"`
|
PeerIP string `json:"peerIP,omitempty"`
|
||||||
@@ -298,6 +299,7 @@ type AppInfo struct {
|
|||||||
RelayMode string `json:"relayMode,omitempty"`
|
RelayMode string `json:"relayMode,omitempty"`
|
||||||
Version string `json:"version,omitempty"`
|
Version string `json:"version,omitempty"`
|
||||||
RetryTime string `json:"retryTime,omitempty"`
|
RetryTime string `json:"retryTime,omitempty"`
|
||||||
|
ConnectTime string `json:"connectTime,omitempty"`
|
||||||
IsActive int `json:"isActive,omitempty"`
|
IsActive int `json:"isActive,omitempty"`
|
||||||
Enabled int `json:"enabled,omitempty"`
|
Enabled int `json:"enabled,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user