Skip to content

Commit

Permalink
Merge pull request #2 from shanghaobo/dev
Browse files Browse the repository at this point in the history
集成frp内网穿透实现公网可调用
  • Loading branch information
shanghaobo authored Aug 24, 2023
2 parents badb780 + 60a2d8a commit 3e3a93d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 10 deletions.
59 changes: 56 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
- [x] 开机启动
- [x] 托盘控制
- [x] 端口配置
- [x] frp内网穿透集成
- [x] 公网调用

Future:
- [ ] Server端

## 架构

![](images/jiagou.png)

其中server部分还未开发完成
- 内网环境下,局域网内可通过内网ip+端口调用,如`http://192.168.124.11:19000`
- 公网环境可选择开启(默认关闭),配置开启后公网内可通过`frps`所在服务器公网ip+映射端口调用,如`http://123.123.1.2:19001`,此时请求经过公网服务器的frps服务转发给本机的frpc,frpc再将请求转发给本地web服务从而触发消息通知。详情查看[开启frp内网穿透](#开启frp内网穿透)


## 使用

Expand Down Expand Up @@ -73,4 +75,55 @@ Future:
![](images/demo1.png)


## 开启frp内网穿透

- 开启frp需要有一台带公网ip的服务器,配置并开启后可实现公网调用接口发送通知。

- 客户端内已集成了`frpc(v0.51.3)`

#### 服务端配置

1. 下载`frps`,选择对应服务器版本即可。[下载地址](https://github.com/fatedier/frp/releases/tag/v0.51.3)
2. 修改`frps.ini`配置文件,配置参考:

- frps.ini
```ini
[common]
bind_port = 7000
token = httpwinnotice123456
```
3. 启动服务端:

```bash
chmod +x ./frps
./frps -c frps.ini
```

#### 客户端配置

1. 右击托盘图标选择`配置文件`,修改配置文件内`frp`相关内容并保存。参考配置如下:

```yaml
frp:
enable: 1
server_addr: 123.123.1.2
server_port: 7000
token: httpwinnotice123456
remote_port: 19001
```
配置的各项解释如下:
- enable: 1-开启frp,0-关闭frp
- server_addr: 部署frps服务器的公网ip
- server_port: frps服务端口号
- token: 与服务器端配置token一致,建议将默认值修改
- remote_port: 转发的远端端口号
注意服务器防火墙放开对应的端口号,以上示例配置在公网调用时使用`http://123.123.1.2:19001/api/toast`

配置好后重启程序即可

#### 测试

浏览器打开`http://123.123.1.2:19001/api/toast?msg=哈喽` 如果windows通知出现证明开启成功(ip和端口号替换为自己的)
78 changes: 78 additions & 0 deletions frp/frp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package frp

import (
"embed"
"fmt"
"http-win-notice/utils"
"http-win-notice/utils/setting"
"log"
"os"
"os/exec"
"path"
"strconv"
)

//go:embed frpc.exe
var frpClient embed.FS

const FrpConfigTemplate = `[common]
server_addr = ${ServerAddr}
server_port = ${ServerPort}
token = ${Token}
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = ${LocalPort}
remote_port = ${RemotePort}`

func initFrpConfig() {
var config = map[string]string{
"ServerAddr": setting.Config.Frp.ServerAddr,
"ServerPort": strconv.Itoa(setting.Config.Frp.ServerPort),
"Token": setting.Config.Frp.Token,
"LocalPort": strconv.Itoa(setting.Config.Port),
"RemotePort": strconv.Itoa(setting.Config.Frp.RemotePort),
}
s := os.Expand(FrpConfigTemplate, func(k string) string {
return config[k]
})
configPath := path.Join(utils.RootDir, "frpc.ini")
err := os.WriteFile(configPath, []byte(s), 0644)
if err != nil {
log.Fatalln("frp配置生成失败", err)
}
}

func initFrp() {
frpPath := path.Join(utils.RootDir, "frpc.exe")
if _, err := os.Stat(frpPath); os.IsNotExist(err) {
frpExeData, _ := frpClient.ReadFile("frpc.exe")
err = os.WriteFile(frpPath, frpExeData, 0755)
if err != nil {
log.Fatalln("frp创建失败", err)
}
}

}

func StartFrp() {
go func() {
if setting.Config.Frp.Enable != 1 {
return
}
initFrp()
initFrpConfig()
frpExePath := path.Join(utils.RootDir, "frpc.exe")
configPath := path.Join(utils.RootDir, "frpc.ini")
fmt.Println("frp path=", frpExePath)
cmd := exec.Command(frpExePath, "-c", configPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalln("frp启动失败", err)
}
}()

}
Binary file added frp/frpc.exe
Binary file not shown.
Binary file modified images/jiagou.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/getlantern/systray"
"github.com/skratchdot/open-golang/open"
"http-win-notice/frp"
"http-win-notice/model"
"http-win-notice/utils/comm"
"http-win-notice/utils/icon"
Expand All @@ -14,6 +15,7 @@ import (
func init() {
comm.InitLog()
model.InitDb()
frp.StartFrp()
}

func onReady() {
Expand Down
27 changes: 20 additions & 7 deletions utils/setting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ var DbPath string
var LogoPath string

type ConfigType struct {
Mode int `yaml:"mode"`
Port int `yaml:"port"`
Port int `yaml:"port"`
Frp FrpType `yaml:"frp"`
}

type FrpType struct {
Enable int `yaml:"enable"`
ServerAddr string `yaml:"server_addr"`
ServerPort int `yaml:"server_port"`
Token string `yaml:"token"`
RemotePort int `yaml:"remote_port"`
}

func init() {
Expand All @@ -38,8 +46,17 @@ func init() {
}

func initConfigFile(ConfigPath string) {
Config.Mode = 1
//默认配置
Config.Port = 19000
Frp := FrpType{
Enable: 0,
ServerAddr: "127.0.0.1",
ServerPort: 7000,
Token: "httpwinnotice123456",
RemotePort: 19001,
}
Config.Frp = Frp

updatedData, err := yaml.Marshal(Config)
if err != nil {
fmt.Println(err)
Expand All @@ -61,10 +78,6 @@ func InitConfig() {
fmt.Println("解析config失败")
log.Fatalln("解析config失败")
}
fmt.Println("config=")
fmt.Println(Config)
fmt.Println(Config.Mode)
fmt.Println(Config.Port)
}

func PortStr() string {
Expand Down

0 comments on commit 3e3a93d

Please sign in to comment.