mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-05-07 13:52:14 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7686af39e0 | ||
|
|
16b937ebd7 | ||
|
|
ac454ec694 |
43
USAGE-ZH.md
43
USAGE-ZH.md
@@ -31,7 +31,48 @@
|
||||
>* -protocol: 目标服务协议 tcp、udp
|
||||
>* -peeruser: 目标用户,如果是同一个用户下的节点,则无需设置
|
||||
>* -peerpassword: 目标密码,如果是同一个用户下的节点,则无需设置
|
||||
>* -f: 配置文件,如果希望配置多个P2PApp参考[config.json](/config.json)
|
||||
|
||||
## 配置文件
|
||||
一般保存在当前目录,安装模式下会保存到 `C:\Program Files\OpenP2P\config.json` 或 `/usr/local/openp2p/config.json`
|
||||
希望修改参数,或者配置多个P2PApp可手动修改配置文件
|
||||
|
||||
配置实例
|
||||
```
|
||||
{
|
||||
"network": {
|
||||
"Node": "hhd1207-222",
|
||||
"User": "USERNAME1",
|
||||
"Password": "PASSWORD1",
|
||||
"ShareBandwidth": -1,
|
||||
"ServerHost": "api.openp2p.cn",
|
||||
"ServerPort": 27182,
|
||||
"UDPPort1": 27182,
|
||||
"UDPPort2": 27183
|
||||
},
|
||||
"apps": [
|
||||
{
|
||||
"AppName": "OfficeWindowsPC",
|
||||
"Protocol": "tcp",
|
||||
"SrcPort": 23389,
|
||||
"PeerNode": "OFFICEPC1",
|
||||
"DstPort": 3389,
|
||||
"DstHost": "localhost",
|
||||
"PeerUser": "",
|
||||
"PeerPassword": ""
|
||||
},
|
||||
{
|
||||
"AppName": "OfficeServerSSH",
|
||||
"Protocol": "tcp",
|
||||
"SrcPort": 22,
|
||||
"PeerNode": "OFFICEPC1",
|
||||
"DstPort": 22,
|
||||
"DstHost": "192.168.1.5",
|
||||
"PeerUser": "",
|
||||
"PeerPassword": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 升级客户端
|
||||
```
|
||||
|
||||
42
USAGE.md
42
USAGE.md
@@ -32,8 +32,48 @@ Create multiple P2PApp by config file
|
||||
>* -protocol: Target service protocol tcp, udp
|
||||
>* -peeruser: The target user, if it is a node under the same user, no need to set
|
||||
>* -peerpassword: The target password, if it is a node under the same user, no need to set
|
||||
>* -f: Configuration file, if you want to configure multiple P2PApp refer to [config.json](/config.json)
|
||||
|
||||
## Config file
|
||||
Generally saved in the current directory, in installation mode it will be saved to `C:\Program Files\OpenP2P\config.json` or `/usr/local/openp2p/config.json`
|
||||
If you want to modify the parameters, or configure multiple P2PApps, you can manually modify the configuration file
|
||||
|
||||
Configuration example
|
||||
```
|
||||
{
|
||||
"network": {
|
||||
"Node": "hhd1207-222",
|
||||
"User": "USERNAME1",
|
||||
"Password": "PASSWORD1",
|
||||
"ShareBandwidth": -1,
|
||||
"ServerHost": "api.openp2p.cn",
|
||||
"ServerPort": 27182,
|
||||
"UDPPort1": 27182,
|
||||
"UDPPort2": 27183
|
||||
},
|
||||
"apps": [
|
||||
{
|
||||
"AppName": "OfficeWindowsPC",
|
||||
"Protocol": "tcp",
|
||||
"SrcPort": 23389,
|
||||
"PeerNode": "OFFICEPC1",
|
||||
"DstPort": 3389,
|
||||
"DstHost": "localhost",
|
||||
"PeerUser": "",
|
||||
"PeerPassword": ""
|
||||
},
|
||||
{
|
||||
"AppName": "OfficeServerSSH",
|
||||
"Protocol": "tcp",
|
||||
"SrcPort": 22,
|
||||
"PeerNode": "OFFICEPC1",
|
||||
"DstPort": 22,
|
||||
"DstHost": "192.168.1.5",
|
||||
"PeerUser": "",
|
||||
"PeerPassword": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
## Client update
|
||||
```
|
||||
# update local client
|
||||
|
||||
14
config.go
14
config.go
@@ -9,6 +9,8 @@ import (
|
||||
|
||||
var gConf Config
|
||||
|
||||
const IntValueNotSet int = -99999999
|
||||
|
||||
type AppConfig struct {
|
||||
// required
|
||||
AppName string
|
||||
@@ -31,11 +33,11 @@ type AppConfig struct {
|
||||
|
||||
// TODO: add loglevel, maxlogfilesize
|
||||
type Config struct {
|
||||
Network NetworkConfig `json:"network"`
|
||||
Apps []AppConfig `json:"apps"`
|
||||
daemonMode bool
|
||||
logLevel int
|
||||
mtx sync.Mutex
|
||||
Network NetworkConfig `json:"network"`
|
||||
Apps []AppConfig `json:"apps"`
|
||||
LogLevel int
|
||||
|
||||
mtx sync.Mutex
|
||||
}
|
||||
|
||||
func (c *Config) add(app AppConfig) {
|
||||
@@ -78,6 +80,8 @@ func (c *Config) save() {
|
||||
|
||||
func (c *Config) load() error {
|
||||
c.mtx.Lock()
|
||||
c.LogLevel = IntValueNotSet
|
||||
c.Network.ShareBandwidth = IntValueNotSet
|
||||
defer c.mtx.Unlock()
|
||||
data, err := ioutil.ReadFile("config.json")
|
||||
if err != nil {
|
||||
|
||||
@@ -62,7 +62,6 @@ func (d *daemon) run() {
|
||||
break
|
||||
}
|
||||
}
|
||||
args = append(args, "-bydaemon")
|
||||
for {
|
||||
// start worker
|
||||
gLog.Println(LevelINFO, "start worker process")
|
||||
@@ -130,7 +129,7 @@ func install() {
|
||||
logLevel := installFlag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error")
|
||||
installFlag.Parse(os.Args[2:])
|
||||
checkParams(*node, *user, *password)
|
||||
gConf.logLevel = *logLevel
|
||||
gConf.LogLevel = *logLevel
|
||||
gConf.Network.ServerHost = *serverHost
|
||||
gConf.Network.User = *user
|
||||
gConf.Network.Node = *node
|
||||
|
||||
@@ -6,6 +6,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -89,10 +93,23 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
|
||||
}(req)
|
||||
case MsgPushUpdate:
|
||||
update()
|
||||
if gConf.daemonMode {
|
||||
os.Exit(0)
|
||||
targetPath := filepath.Join(defaultInstallPath, defaultBinName)
|
||||
args := []string{"update"}
|
||||
env := os.Environ()
|
||||
// Windows does not support exec syscall.
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd := exec.Command(targetPath, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Env = env
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
os.Exit(0)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return syscall.Exec(targetPath, args, env)
|
||||
case MsgPushReportApps:
|
||||
gLog.Println(LevelINFO, "MsgPushReportApps")
|
||||
req := ReportApps{}
|
||||
@@ -144,22 +161,23 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
gLog.Printf(LevelERROR, "wrong MsgPushEditApp:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
var config AppConfig
|
||||
var oldConf AppConfig
|
||||
// protocol0+srcPort0 exist, delApp
|
||||
config.AppName = newApp.AppName
|
||||
config.Protocol = newApp.Protocol0
|
||||
config.SrcPort = newApp.SrcPort0
|
||||
config.PeerNode = newApp.PeerNode
|
||||
config.DstHost = newApp.DstHost
|
||||
config.DstPort = newApp.DstPort
|
||||
oldConf.AppName = newApp.AppName
|
||||
oldConf.Protocol = newApp.Protocol0
|
||||
oldConf.SrcPort = newApp.SrcPort0
|
||||
oldConf.PeerNode = newApp.PeerNode
|
||||
oldConf.DstHost = newApp.DstHost
|
||||
oldConf.DstPort = newApp.DstPort
|
||||
|
||||
gConf.delete(config)
|
||||
gConf.delete(oldConf)
|
||||
// AddApp
|
||||
config.Protocol = newApp.Protocol
|
||||
config.SrcPort = newApp.SrcPort
|
||||
gConf.add(config)
|
||||
newConf := oldConf
|
||||
newConf.Protocol = newApp.Protocol
|
||||
newConf.SrcPort = newApp.SrcPort
|
||||
gConf.add(newConf)
|
||||
gConf.save()
|
||||
pn.DeleteApp(config) // save quickly for the next request reportApplist
|
||||
pn.DeleteApp(oldConf) // save quickly for the next request reportApplist
|
||||
// autoReconnect will auto AddApp
|
||||
// pn.AddApp(config)
|
||||
// TODO: report result
|
||||
|
||||
21
log.go
21
log.go
@@ -47,7 +47,7 @@ const (
|
||||
type V8log struct {
|
||||
loggers map[LogLevel]*log.Logger
|
||||
files map[LogLevel]*os.File
|
||||
llevel LogLevel
|
||||
level LogLevel
|
||||
stopSig chan bool
|
||||
logDir string
|
||||
mtx *sync.Mutex
|
||||
@@ -92,17 +92,10 @@ func InitLogger(path string, filePrefix string, level LogLevel, maxLogSize int64
|
||||
return pLog
|
||||
}
|
||||
|
||||
// UninitLogger ...
|
||||
func (vl *V8log) UninitLogger() {
|
||||
if !vl.stoped {
|
||||
vl.stoped = true
|
||||
close(vl.stopSig)
|
||||
for l := range logFileNames {
|
||||
if l >= vl.llevel {
|
||||
vl.files[l].Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
func (vl *V8log) setLevel(level LogLevel) {
|
||||
vl.mtx.Lock()
|
||||
defer vl.mtx.Unlock()
|
||||
vl.level = level
|
||||
}
|
||||
|
||||
func (vl *V8log) checkFile() {
|
||||
@@ -150,7 +143,7 @@ func (vl *V8log) Printf(level LogLevel, format string, params ...interface{}) {
|
||||
if vl.stoped {
|
||||
return
|
||||
}
|
||||
if level < vl.llevel {
|
||||
if level < vl.level {
|
||||
return
|
||||
}
|
||||
pidAndLevel := []interface{}{vl.pid, loglevel[level]}
|
||||
@@ -170,7 +163,7 @@ func (vl *V8log) Println(level LogLevel, params ...interface{}) {
|
||||
if vl.stoped {
|
||||
return
|
||||
}
|
||||
if level < vl.llevel {
|
||||
if level < vl.level {
|
||||
return
|
||||
}
|
||||
pidAndLevel := []interface{}{vl.pid, " ", loglevel[level], " "}
|
||||
|
||||
39
openp2p.go
39
openp2p.go
@@ -13,6 +13,8 @@ func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
binDir := filepath.Dir(os.Args[0])
|
||||
os.Chdir(binDir) // for system service
|
||||
gLog = InitLogger(binDir, "openp2p", LevelDEBUG, 1024*1024, LogFileAndConsole)
|
||||
|
||||
// TODO: install sub command, deamon process
|
||||
// groups := flag.String("groups", "", "you could join in several groups. like: GroupName1:Password1;GroupName2:Password2; group name 8-31 characters")
|
||||
if len(os.Args) > 1 {
|
||||
@@ -25,7 +27,7 @@ func main() {
|
||||
update()
|
||||
targetPath := filepath.Join(defaultInstallPath, defaultBinName)
|
||||
d := daemon{}
|
||||
err := d.Control("restart", targetPath, []string{"-d", "-f"})
|
||||
err := d.Control("restart", targetPath, nil)
|
||||
if err != nil {
|
||||
gLog.Println(LevelERROR, "restart service error:", err)
|
||||
} else {
|
||||
@@ -53,11 +55,11 @@ func main() {
|
||||
srcPort := flag.Int("srcport", 0, "source port ")
|
||||
protocol := flag.String("protocol", "tcp", "tcp or udp")
|
||||
appName := flag.String("appname", "", "app name")
|
||||
flag.Bool("noshare", false, "deprecated. uses -sharebandwidth -1")
|
||||
flag.Bool("noshare", false, "deprecated. uses -sharebandwidth -1") // Deprecated, rm later
|
||||
shareBandwidth := flag.Int("sharebandwidth", 10, "N mbps share bandwidth limit, private node no limit")
|
||||
flag.Bool("f", false, "deprecated. config file")
|
||||
flag.Bool("f", false, "deprecated. config file") // Deprecated, rm later
|
||||
daemonMode := flag.Bool("d", false, "daemonMode")
|
||||
byDaemon := flag.Bool("bydaemon", false, "start by daemon")
|
||||
flag.Bool("bydaemon", false, "start by daemon") // Deprecated, rm later
|
||||
logLevel := flag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error")
|
||||
flag.Parse()
|
||||
|
||||
@@ -75,6 +77,7 @@ func main() {
|
||||
gConf.load()
|
||||
gConf.mtx.Lock()
|
||||
|
||||
// spec paramters in commandline will always be used
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
if f.Name == "sharebandwidth" {
|
||||
gConf.Network.ShareBandwidth = *shareBandwidth
|
||||
@@ -92,14 +95,36 @@ func main() {
|
||||
gConf.Network.ServerHost = *serverHost
|
||||
}
|
||||
if f.Name == "loglevel" {
|
||||
gConf.logLevel = *logLevel
|
||||
gConf.LogLevel = *logLevel
|
||||
}
|
||||
})
|
||||
gLog = InitLogger(binDir, "openp2p", LogLevel(gConf.logLevel), 1024*1024, LogFileAndConsole)
|
||||
|
||||
if gConf.Network.ServerHost == "" {
|
||||
gConf.Network.ServerHost = *serverHost
|
||||
}
|
||||
if gConf.Network.Node == "" {
|
||||
gConf.Network.Node = *node
|
||||
}
|
||||
if gConf.Network.User == "" {
|
||||
gConf.Network.User = *user
|
||||
}
|
||||
if gConf.Network.Password == "" {
|
||||
gConf.Network.Password = *password
|
||||
}
|
||||
if gConf.LogLevel == IntValueNotSet {
|
||||
gConf.LogLevel = *logLevel
|
||||
}
|
||||
if gConf.Network.ShareBandwidth == IntValueNotSet {
|
||||
gConf.Network.ShareBandwidth = *shareBandwidth
|
||||
}
|
||||
|
||||
gConf.Network.ServerPort = 27182
|
||||
gConf.Network.UDPPort1 = 27182
|
||||
gConf.Network.UDPPort2 = 27183
|
||||
gLog.Println(LevelINFO, "openp2p start. version: ", OpenP2PVersion)
|
||||
gLog.setLevel(LogLevel(gConf.LogLevel))
|
||||
gConf.mtx.Unlock()
|
||||
gConf.save()
|
||||
gConf.daemonMode = *byDaemon
|
||||
if *daemonMode {
|
||||
d := daemon{}
|
||||
d.run()
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const OpenP2PVersion = "0.98.0"
|
||||
const OpenP2PVersion = "0.99.0"
|
||||
const ProducnName string = "openp2p"
|
||||
|
||||
type openP2PHeader struct {
|
||||
|
||||
16
update.go
16
update.go
@@ -16,18 +16,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// type updateFileInfo struct {
|
||||
// Name string `json:"name,omitempty"`
|
||||
// RelativePath string `json:"relativePath,omitempty"`
|
||||
// Length int64 `json:"length,omitempty"`
|
||||
// URL string `json:"url,omitempty"`
|
||||
// Hash string `json:"hash,omitempty"`
|
||||
// }
|
||||
|
||||
func update() {
|
||||
gLog.Println(LevelINFO, "update start")
|
||||
defer gLog.Println(LevelINFO, "update end")
|
||||
// TODO: download from gitee. save flow
|
||||
c := http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
@@ -61,7 +52,6 @@ func update() {
|
||||
gLog.Println(LevelERROR, "update error:", updateInfo.Error, updateInfo.ErrorDetail)
|
||||
return
|
||||
}
|
||||
os.MkdirAll("download", 0666)
|
||||
err = updateFile(updateInfo.Url, "", "openp2p")
|
||||
if err != nil {
|
||||
gLog.Println(LevelERROR, "update: download failed:", err)
|
||||
@@ -112,6 +102,7 @@ func updateFile(url string, checksum string, dst string) error {
|
||||
os.Rename(os.Args[0]+"0", os.Args[0])
|
||||
return err
|
||||
}
|
||||
os.Remove(tmpFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -133,11 +124,6 @@ func unzip(dst, src string) (err error) {
|
||||
for _, f := range archive.File {
|
||||
filePath := filepath.Join(dst, f.Name)
|
||||
fmt.Println("unzipping file ", filePath)
|
||||
|
||||
// if !strings.HasPrefix(filePath, filepath.Clean(dst)+string(os.PathSeparator)) {
|
||||
// fmt.Println("invalid file path")
|
||||
// return
|
||||
// }
|
||||
if f.FileInfo().IsDir() {
|
||||
fmt.Println("creating directory...")
|
||||
os.MkdirAll(filePath, os.ModePerm)
|
||||
|
||||
Reference in New Issue
Block a user