Skip to content

Commit

Permalink
web console
Browse files Browse the repository at this point in the history
  • Loading branch information
TenderIronh committed Feb 3, 2022
1 parent 7686af3 commit 95b46f5
Show file tree
Hide file tree
Showing 17 changed files with 410 additions and 347 deletions.
57 changes: 31 additions & 26 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,43 @@ P2P直连可以让你的设备跑满带宽。不论你的设备在任何网络
基于OpenP2P只需数行代码,就能让原来只能局域网通信的程序,变成任何内网都能通信

## 快速入门
仅需简单4步就能用起来。
下面是一个远程办公例子:在家里连入办公室Windows电脑。
### 1.注册
前往<https://console.openp2p.cn> 注册新用户,暂无需任何认证

![image](/doc/images/register.png)
### 2.安装
分别在本地和远程电脑下载后双击运行,一键安装

![image](/doc/images/install.png)

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

![image](/doc/images/win10warn.png)

![image](/doc/images/stillrun.png)
### 3.新建P2P应用

![image](/doc/images/devices.png)

![image](/doc/images/newapp.png)

![image](/doc/images/newappedit.png)

### 4.使用P2P应用
在“MyHomePC”设备上能看到刚才创建的P2P应用,连接下图显示的“本地监听端口”即可。

![image](/doc/images/p2pappok.png)

在家里Windows电脑,按Win+R输入mstsc打开远程桌面,输入127.0.0.1:23389 /admin

> :warning: 本文所有命令, Windows环境使用"openp2p.exe", Linux环境使用"./openp2p"

以一个最常见的例子说明OpenP2P如何使用:远程办公,在家里连入办公室Windows电脑。
相信很多人在疫情下远程办公是刚需。
1. 先确认办公室电脑已开启远程桌面功能(如何开启参考官方说明https://docs.microsoft.com/zh-cn/windows-server/remote/remote-desktop-services/clients/remote-desktop-allow-access)
2. 在办公室下载最新的`OpenP2P`[下载页](https://openp2p.cn/),解压出来,在命令行执行
```
openp2p.exe install -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
```

> :warning: **切记将标记大写的参数改成自己的,3个参数的长度必须>=8个字符**
![image](/doc/images/officelisten.png)
3. 在家里下载最新的OpenP2P,解压出来,在命令行执行
```
openp2p.exe -d -node HOMEPC123 -user USERNAME1 -password PASSWORD1 -appname WindowsRemote -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 3389 -srcport 23389 -protocol tcp
```
> :warning: **切记将标记大写的参数改成自己的**
![image](/doc/images/homeconnect.png)
![image](/doc/images/mem.png)
`LISTEN ON PORT 23389 START` 看到这行日志表示P2PApp建立成功,监听23389端口。只需连接本机的127.0.0.1:23389就相当于连接公司Windows电脑的3389端口。

4. 在家里Windows电脑,按Win+R输入mstsc打开远程桌面,输入127.0.0.1:23389 /admin
![image](/doc/images/mstscconnect.png)

![image](/doc/images/afterconnect.png)

## 详细使用说明
[这里](/USAGE-ZH.md)详细介绍如何使用和运行参数
[这里](/USAGE-ZH.md)介绍如何手动运行

## 典型应用场景
特别适合大流量的内网访问
Expand Down
61 changes: 27 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,45 @@ P2P direct connection lets your devices make good use of bandwidth. Your device
Your applicaiton can call OpenP2P with a few code to make any internal networks communicate with each other.

## Get Started
A common scenario to introduce OpenP2P: remote work. At home connects to office's Linux PC .
Under the outbreak of covid-19 pandemic, surely remote work becomes a fundamental demand.
Just 4 simple steps to use.
Here's an example of remote work: connecting to an office Windows computer at home.

### 1.Register
Go to <https://console.openp2p.cn> register a new user

![image](/doc/images/register.png)
### 2.Install
Download on local and remote computers and double-click to run, one-click installation

![image](/doc/images/install.png)

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

![image](/doc/images/win10warn.png)

> :warning: all commands in this doc, Windows env uses "openp2p.exe", Linux env uses "./openp2p"
![image](/doc/images/stillrun.png)
### 3.New P2PApp

1. Make sure your office device(Linux) has opened the access of ssh.
```
netstat -nl | grep 22
```
Output sample
![image](/doc/images/officelisten_linux.png)
![image](/doc/images/devices.png)

2. Download the latest version of `OpenP2P` [Download Page](https://openp2p.cn/),unzip the downloaded package, and execute below command line.
```
tar xzvf ${PackageName}
./openp2p install -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
```
![image](/doc/images/newapp.png)

> :warning: **Must change the parameters marked in UPPERCASE to your own. These 3 parameters must >= 8 charaters**
![image](/doc/images/newappedit.png)

Output sample
![image](/doc/images/officeexecute_linux.png)
### 4.Use P2PApp
You can see the P2P application you just created on the "MyHomePC" device, just connect to the "local listening port" shown in the figure below.

3. Download OpenP2P on your home device,unzip and execute below command line.
```
openp2p.exe -d -node HOMEPC123 -user USERNAME1 -password PASSWORD1 -appname OfficeSSH -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 22 -srcport 22022 -protocol tcp
```

> :warning: **Must change the parameters marked in UPPERCASE to your own**
![image](/doc/images/p2pappok.png)

Output sample
![image](/doc/images/homeconnect_windows.png)
The log of `LISTEN ON PORT 22022 START` indicates P2PApp runs successfully on your home device, listing port is 22022. Once connects to local ip:port,127.0.0.1:22022, it means the home device has conneccted to the office device's port, 22.
![image](/doc/images/officelisten_2_linux.png)
On MyHomePC, press Win+R and enter MSTSC to open the remote desktop, input `127.0.0.1:23389 /admin`

![image](/doc/images/mstscconnect.png)

4. Test the connection between office device and home device.In your home deivce, run SSH to login the office device.
```
ssh -p22022 [email protected]:22022
```
![image](/doc/images/sshconnect.png)
![image](/doc/images/afterconnect.png)


## Usage
[Here](/USAGE.md) is a detailed description of how to use and running parameters
[Here](/USAGE.md) describes how to run manually

## Scenarios
Especially suitable for large traffic intranet access.
Expand Down
26 changes: 9 additions & 17 deletions USAGE-ZH.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# 详细运行参数说明
# 手动运行说明
大部分情况通过<https://console.openp2p.cn> 操作即可。有些情况需要手动运行
> :warning: 本文所有命令, Windows环境使用"openp2p.exe", Linux环境使用"./openp2p"

## 安装和监听
```
./openp2p install -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
./openp2p install -node OFFICEPC1 -token TOKEN
./openp2p -d -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
./openp2p -d -node OFFICEPC1 -token TOKEN
# 注意Windows系统把“./openp2p” 换成“openp2p.exe”
```
>* install: 安装模式【推荐】,会安装成系统服务,这样它就能随系统自动启动
>* -d: daemon模式。发现worker进程意外退出就会自动启动新的worker进程
>* -node: 独一无二的节点名字,唯一标识
>* -user: 独一无二的用户名字,该节点属于这个user
>* -password: 密码
>* -token: 在<console.openp2p.cn>“我的”里面找到
>* -sharebandwidth: 作为共享节点时提供带宽,默认10mbps. 如果是光纤大带宽,设置越大效果越好. -1表示不共享,该节点只在私有的P2P网络使用。不加入共享的P2P网络,这样也意味着无法使用别人的共享节点
>* -loglevel: 需要查看更多调试日志,设置0;默认是1
## 连接
```
./openp2p -d -node HOMEPC123 -user USERNAME1 -password PASSWORD1 -appname OfficeWindowsRemote -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 3389 -srcport 23389 -protocol tcp
./openp2p -d -node HOMEPC123 -token TOKEN -appname OfficeWindowsRemote -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 3389 -srcport 23389
使用配置文件,建立多个P2PApp
./openp2p -d -f
./openp2p -f
./openp2p -d
```
>* -appname: 这个P2P应用名字
>* -peernode: 目标节点名字
>* -dstip: 目标服务地址,默认本机127.0.0.1
>* -dstport: 目标服务端口,常见的如windows远程桌面3389,Linux ssh 22
>* -protocol: 目标服务协议 tcp、udp
>* -peeruser: 目标用户,如果是同一个用户下的节点,则无需设置
>* -peerpassword: 目标密码,如果是同一个用户下的节点,则无需设置
## 配置文件
一般保存在当前目录,安装模式下会保存到 `C:\Program Files\OpenP2P\config.json``/usr/local/openp2p/config.json`
Expand All @@ -41,11 +38,10 @@
{
"network": {
"Node": "hhd1207-222",
"User": "USERNAME1",
"Password": "PASSWORD1",
"Token": "TOKEN",
"ShareBandwidth": -1,
"ServerHost": "api.openp2p.cn",
"ServerPort": 27182,
"ServerPort": 27183,
"UDPPort1": 27182,
"UDPPort2": 27183
},
Expand All @@ -57,8 +53,6 @@
"PeerNode": "OFFICEPC1",
"DstPort": 3389,
"DstHost": "localhost",
"PeerUser": "",
"PeerPassword": ""
},
{
"AppName": "OfficeServerSSH",
Expand All @@ -67,8 +61,6 @@
"PeerNode": "OFFICEPC1",
"DstPort": 22,
"DstHost": "192.168.1.5",
"PeerUser": "",
"PeerPassword": ""
}
]
}
Expand Down
27 changes: 10 additions & 17 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
# Parameters details


# Parameters details
In most cases, you can operate it through <https://console.openp2p.cn>. In some cases it is necessary to run manually
> :warning: all commands in this doc, Windows env uses "openp2p.exe", Linux env uses "./openp2p"

## Install and Listen
```
./openp2p install -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
./openp2p install -node OFFICEPC1 -token TOKEN
Or
./openp2p -d -node OFFICEPC1 -user USERNAME1 -password PASSWORD1
./openp2p -d -node OFFICEPC1 -token TOKEN
```
>* install: [recommand] will install as system service. So it will autorun when system booting.
>* -d: daemon mode run once. When the worker process is found to exit unexpectedly, a new worker process will be automatically started
>* -node: Unique node name, unique identification
>* -user: Unique user name, the node belongs to this user
>* -password: Password
>* -token: See <console.openp2p.cn> "Profile"
>* -sharebandwidth: Provides bandwidth when used as a shared node, the default is 10mbps. If it is a large bandwidth of optical fiber, the larger the setting, the better the effect. -1 means not shared, the node is only used in a private P2P network. Do not join the shared P2P network, which also means that you CAN NOT use other people’s shared nodes
>* -loglevel: Need to view more debug logs, set 0; the default is 1
## Connect
```
./openp2p -d -node HOMEPC123 -user USERNAME1 -password PASSWORD1 -appname OfficeWindowsRemote -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 3389 -srcport 23389 -protocol tcp
./openp2p -d -node HOMEPC123 -token TOKEN -appname OfficeWindowsRemote -peernode OFFICEPC1 -dstip 127.0.0.1 -dstport 3389 -srcport 23389
Create multiple P2PApp by config file
./openp2p -d -f
./openp2p -f
./openp2p -d
```
>* -appname: This P2PApp name
>* -peernode: Target node name
>* -dstip: Target service address, default local 127.0.0.1
>* -dstport: Target service port, such as windows remote desktop 3389, Linux ssh 22
>* -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
## 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`
Expand All @@ -42,11 +40,10 @@ Configuration example
{
"network": {
"Node": "hhd1207-222",
"User": "USERNAME1",
"Password": "PASSWORD1",
"Token": "TOKEN",
"ShareBandwidth": -1,
"ServerHost": "api.openp2p.cn",
"ServerPort": 27182,
"ServerPort": 27183,
"UDPPort1": 27182,
"UDPPort2": 27183
},
Expand All @@ -58,8 +55,6 @@ Configuration example
"PeerNode": "OFFICEPC1",
"DstPort": 3389,
"DstHost": "localhost",
"PeerUser": "",
"PeerPassword": ""
},
{
"AppName": "OfficeServerSSH",
Expand All @@ -68,8 +63,6 @@ Configuration example
"PeerNode": "OFFICEPC1",
"DstPort": 22,
"DstHost": "192.168.1.5",
"PeerUser": "",
"PeerPassword": ""
}
]
}
Expand Down
38 changes: 19 additions & 19 deletions bandwidthLimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ import (

// BandwidthLimiter ...
type BandwidthLimiter struct {
freeFlowTime time.Time
bandwidth int // mbps
freeFlow int // bytes
maxFreeFlow int // bytes
freeFlowMtx sync.Mutex
ts time.Time
bw int // mbps
freeBytes int // bytes
maxFreeBytes int // bytes
mtx sync.Mutex
}

// mbps
func newBandwidthLimiter(bw int) *BandwidthLimiter {
return &BandwidthLimiter{
bandwidth: bw,
freeFlowTime: time.Now(),
maxFreeFlow: bw * 1024 * 1024 / 8,
freeFlow: bw * 1024 * 1024 / 8,
bw: bw,
ts: time.Now(),
maxFreeBytes: bw * 1024 * 1024 / 8,
freeBytes: bw * 1024 * 1024 / 8,
}
}

// Add ...
func (bl *BandwidthLimiter) Add(bytes int) {
if bl.bandwidth <= 0 {
if bl.bw <= 0 {
return
}
bl.freeFlowMtx.Lock()
defer bl.freeFlowMtx.Unlock()
bl.mtx.Lock()
defer bl.mtx.Unlock()
// calc free flow 1000*1000/1024/1024=0.954; 1024*1024/1000/1000=1.048
bl.freeFlow += int(time.Now().Sub(bl.freeFlowTime) * time.Duration(bl.bandwidth) / 8 / 954)
if bl.freeFlow > bl.maxFreeFlow {
bl.freeFlow = bl.maxFreeFlow
bl.freeBytes += int(time.Since(bl.ts) * time.Duration(bl.bw) / 8 / 954)
if bl.freeBytes > bl.maxFreeBytes {
bl.freeBytes = bl.maxFreeBytes
}
bl.freeFlow -= bytes
bl.freeFlowTime = time.Now()
if bl.freeFlow < 0 {
bl.freeBytes -= bytes
bl.ts = time.Now()
if bl.freeBytes < 0 {
// sleep for the overflow
time.Sleep(time.Millisecond * time.Duration(-bl.freeFlow/(bl.bandwidth*1048/8)))
time.Sleep(time.Millisecond * time.Duration(-bl.freeBytes/(bl.bw*1048/8)))
}
}
Loading

0 comments on commit 95b46f5

Please sign in to comment.