Skip to content

Commit 81c237e

Browse files
author
dushixiang
committed
Initial commit
0 parents  commit 81c237e

18 files changed

+1721
-0
lines changed

.github/workflows/release.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Run GoReleaser
14+
uses: goreleaser/goreleaser-action@v2
15+
with:
16+
go-version: 1.21
17+
version: latest
18+
args: release --rm-dist
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.goreleaser.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
builds:
2+
- env:
3+
- CGO_ENABLED=0
4+
goos:
5+
- linux
6+
- windows
7+
- darwin
8+
goarch:
9+
- arm
10+
- arm64
11+
- 386
12+
- amd64
13+
- mips
14+
- mipsle
15+
checksum:
16+
name_template: "checksums.txt"

README_zh.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Meteor
2+
3+
## 使用
4+
5+
**安装**
6+
```shell
7+
meteor install
8+
```
9+
10+
**修改配置文件**
11+
12+
```shell
13+
vim /etc/meteor/meteor.yaml
14+
```
15+
16+
配置文件示例:
17+
```shell
18+
geoip:
19+
file: GeoLite2-City.mmdb # 配置geoip后支持按城市配置规则
20+
forwarders:
21+
- protocol: tcp # 仅支持 tcp 和 udp
22+
addr: ":54321" # 本机监听地址
23+
to: 127.0.0.1:12345 # 目标地址
24+
rules:
25+
- city: beijing,成都 # 城市,支持中文、拼音
26+
allowed: true # 是否允许访问 ✅
27+
- ip: 0.0.0.0/0 # 0.0.0.0/0 代表全部的IP地址
28+
allowed: false # 这个配置的含义就是只允许 beijing和成都的IP地址访问,其他的全部禁止访问。🈲
29+
- protocol: udp
30+
addr: ":54321"
31+
to: 127.0.0.1:12345
32+
proxies:
33+
- protocol: http # 仅支持 http、https、socks5
34+
addr: 127.0.0.1:80 # 本地监听地址
35+
auth: true # 是否开启认证
36+
accounts: # 账户列表
37+
- username: a # 账号
38+
password: b # 密码
39+
- protocol: https
40+
addr: 127.0.0.1:443
41+
key: /root/key.pem # https key path
42+
cert: /root/cert.pem # https cert path
43+
auth: true # 是否开启认证
44+
accounts: # 账户列表
45+
- username: a # 账号
46+
password: b # 密码
47+
- protocol: socks5
48+
addr: 127.0.0.1:1080
49+
auth: true # 是否开启认证
50+
accounts: # 账户列表
51+
- username: a # 账号
52+
password: b # 密码
53+
```
54+
55+
启动
56+
```shell
57+
meteor start
58+
```
59+
60+
停止
61+
```shell
62+
meteor stop
63+
```
64+
65+
卸载
66+
```shell
67+
meteor uninstall
68+
```
69+
70+
## 其他参数
71+
72+
`meteor -h`
73+
74+
```shell
75+
Meteor is a network tool that can quickly forward tcp and udp ports and start http, https and socks5 proxy servers.
76+
77+
Usage:
78+
meteor [flags]
79+
meteor [command]
80+
81+
Available Commands:
82+
completion Generate the autocompletion script for the specified shell
83+
forward Forward the received data to the destination address
84+
help Help about any command
85+
install Install meteor as a system service
86+
proxy Start a proxy server
87+
restart Restart meteor system service
88+
start Start meteor system service
89+
stop Stop meteor system service
90+
uninstall Uninstall meteor system service
91+
version Show version
92+
93+
Flags:
94+
-c, --config string -c /path/config.yaml (default "/etc/meteor/meteor.yaml")
95+
-d, --debug print debug log
96+
-h, --help help for meteor
97+
98+
Use "meteor [command] --help" for more information about a command.
99+
```

build_local_test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o meteor_linux_amd64 main.go
2+
#CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o meteor_windows_amd64.exe main.go
3+
#CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o meteor_darwin_amd64 main.go
4+
#CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w" -o meteor_darwin_arm64 main.go
5+
6+
upx meteor_linux_amd64
7+
#upx meteor_windows_amd64.exe
8+
#upx meteor_darwin_amd64
9+
#upx meteor_darwin_arm64

cmd/commands.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
6+
"github.com/dushxiiang/meteor/meteor"
7+
"github.com/dushxiiang/meteor/pkg/logger"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var forwardModel meteor.Forwarder
13+
var forwardCmd = &cobra.Command{
14+
Use: "forward",
15+
SuggestFor: []string{"f"},
16+
Short: "Forward the received data to the destination address",
17+
Args: cobra.MinimumNArgs(2),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
logger.Init(debug)
20+
ctx := context.Background()
21+
forwardModel.Addr = args[0]
22+
forwardModel.To = args[1]
23+
forwardModel.Forward(ctx)
24+
},
25+
}
26+
27+
var proxyModel meteor.Proxy
28+
var proxyCmd = &cobra.Command{
29+
Use: "proxy",
30+
Short: "Start a proxy server",
31+
Args: cobra.MinimumNArgs(1),
32+
Run: func(cmd *cobra.Command, args []string) {
33+
logger.Init(debug)
34+
ctx := context.Background()
35+
proxyModel.Addr = args[0]
36+
if len(args) == 3 {
37+
proxyModel.Cert = args[1]
38+
proxyModel.Key = args[2]
39+
}
40+
proxyModel.Run(ctx)
41+
},
42+
}
43+
44+
var versionCmd = &cobra.Command{
45+
Use: "version",
46+
Short: "Show version",
47+
Run: func(cmd *cobra.Command, args []string) {
48+
println(meteor.Version)
49+
},
50+
}
51+
52+
func init() {
53+
rootCmd.AddCommand(versionCmd)
54+
55+
forwardCmd.PersistentFlags().StringVarP(&forwardModel.Protocol, "protocol", "p", "tcp", "tcp|udp")
56+
forwardCmd.PersistentFlags().StringVarP(&forwardModel.Addr, "addr", "a", "", "8080|0.0.0.0:880")
57+
forwardCmd.PersistentFlags().StringVarP(&forwardModel.To, "to", "t", "", "127.0.0.1:80")
58+
rootCmd.AddCommand(forwardCmd)
59+
60+
proxyCmd.PersistentFlags().StringVarP(&proxyModel.Protocol, "protocol", "p", "http", "protocol: http|https|socks5")
61+
proxyCmd.PersistentFlags().StringVarP(&proxyModel.Addr, "addr", "a", "", "8080|0.0.0.0:880")
62+
proxyCmd.PersistentFlags().StringVar(&proxyModel.Cert, "cert", "", "only for https, /path/cert.pem")
63+
proxyCmd.PersistentFlags().StringVar(&proxyModel.Key, "key", "", "only for https, /path/key.pem")
64+
65+
rootCmd.AddCommand(proxyCmd)
66+
67+
rootCmd.AddCommand(installCmd)
68+
rootCmd.AddCommand(uninstallCmd)
69+
rootCmd.AddCommand(startCmd)
70+
rootCmd.AddCommand(stopCmd)
71+
rootCmd.AddCommand(restartCmd)
72+
}

cmd/install.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/dushxiiang/meteor/meteor"
10+
"github.com/dushxiiang/meteor/pkg/logger"
11+
12+
"github.com/kardianos/service"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var defaultConfig = `
17+
//geoip:
18+
// file: /etc/meteor/GeoLite2-City.mmdb
19+
forwarders:
20+
- protocol: tcp
21+
addr: ":8080"
22+
to: 127.0.0.1:80
23+
rules:
24+
- city: beijing,chengdu
25+
allowed: true
26+
- ip: 0.0.0.0/0
27+
allowed: false
28+
# - protocol: udp
29+
# addr: ":54321"
30+
# to: 127.0.0.1:12345
31+
#proxies:
32+
# - protocol: http
33+
# addr: 127.0.0.1:8080
34+
# auth: true
35+
# accounts:
36+
# - username: a
37+
# password: b
38+
# - protocol: https
39+
# addr: 127.0.0.1:80
40+
# key: /root/key.pem
41+
# cert: /root/cert.pem
42+
# - protocol: socks5
43+
# addr: 127.0.0.1:1080
44+
`
45+
46+
func createConfigIfNotExists(filePath, exampleConfig string) error {
47+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
48+
dir := filepath.Dir(filePath)
49+
if err := os.MkdirAll(dir, 0755); err != nil {
50+
return err
51+
}
52+
53+
if err := os.WriteFile(filePath, []byte(exampleConfig), 0755); err != nil {
54+
return err
55+
}
56+
} else if err != nil {
57+
return err
58+
}
59+
60+
return nil
61+
}
62+
63+
var installCmd = &cobra.Command{
64+
Use: "install",
65+
Short: "Install meteor as a system service",
66+
Run: func(cmd *cobra.Command, args []string) {
67+
logger.Init(debug)
68+
err := createConfigIfNotExists(config, defaultConfig)
69+
if err != nil {
70+
logger.L.Sugar().Fatal("create config file", err)
71+
}
72+
control(config, cmd.Use)
73+
},
74+
}
75+
76+
var uninstallCmd = &cobra.Command{
77+
Use: "uninstall",
78+
Short: "Uninstall meteor system service",
79+
Run: func(cmd *cobra.Command, args []string) {
80+
logger.Init(debug)
81+
control(config, cmd.Use)
82+
},
83+
}
84+
85+
var startCmd = &cobra.Command{
86+
Use: "start",
87+
Short: "Start meteor system service",
88+
Run: func(cmd *cobra.Command, args []string) {
89+
logger.Init(debug)
90+
control(config, cmd.Use)
91+
},
92+
}
93+
94+
var stopCmd = &cobra.Command{
95+
Use: "stop",
96+
Short: "Stop meteor system service",
97+
Run: func(cmd *cobra.Command, args []string) {
98+
logger.Init(debug)
99+
control(config, cmd.Use)
100+
},
101+
}
102+
103+
var restartCmd = &cobra.Command{
104+
Use: "restart",
105+
Short: "Restart meteor system service",
106+
Run: func(cmd *cobra.Command, args []string) {
107+
logger.Init(debug)
108+
control(config, cmd.Use)
109+
},
110+
}
111+
112+
func control(config, action string) {
113+
srv, err := getService(config)
114+
if err != nil {
115+
fmt.Printf("err: %v\n", err)
116+
return
117+
}
118+
if err := service.Control(srv, action); err != nil {
119+
fmt.Printf("%v\n", err)
120+
return
121+
}
122+
fmt.Printf("success\n")
123+
}
124+
125+
func getService(config string) (service.Service, error) {
126+
svcConfig := &service.Config{
127+
Name: "meteor",
128+
DisplayName: "meteor service",
129+
Description: "This is meteor service.",
130+
Arguments: []string{"-c", config},
131+
Dependencies: []string{
132+
"Requires=network.target",
133+
"After=network-online.target syslog.target",
134+
},
135+
Option: service.KeyValue{
136+
"Restart": "always",
137+
},
138+
}
139+
140+
if debug {
141+
svcConfig.Arguments = append(svcConfig.Arguments, "-d")
142+
}
143+
144+
prg, err := meteor.New(config)
145+
if err != nil {
146+
return nil, errors.New("error to new meteor, " + err.Error())
147+
}
148+
return service.New(prg, svcConfig)
149+
}

0 commit comments

Comments
 (0)