An apple a day keeps the doctor away !

0%

Golang与Docker实现联通809订阅快速部署

Docker 740162752/unicom809 Golang开源

!仅供学习Go语言交流使用,请勿用于商业用途。

感谢Yuzuki616。在Yuzuki616大佬的基础上加上web监听便于生成订阅链接自动更新;打包成docker便于部署。Docker:740162752/unicom809

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"strings"
"time"
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

var listen = flag.String("l", ":8000", "订阅的监听端口")

var yport = flag.String("p", "443", "原服务的端口")

var yhost = flag.String("h", "", "原服务的域名或ip,默认空")

var yuuid = flag.String("u", "", "原服务的uuid,默认空")

var route = flag.String("r", "app", "访问订阅的路由")

func getFakeID() string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
strb := []byte(str)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var result []byte
for i := 0; i < 22; i++ {
result = append(result, strb[r.Intn(len(strb))])
}
return string(result)
}

func GetBody(ip, port string) []byte {
path := "if5ax/?fakeid=" + getFakeID() + "&spid=81117&pid=81117&spip=" + ip + "&spport=" + port

host := "http://dir.v.wo.cn:809/"
m := md5.Sum([]byte(path + "3d99ff138e1f41e931e58617e7d128e2"))
key := hex.EncodeToString(m[:])
r, _ := http.Get(host + path + "&spkey=" + key)
body, _ := io.ReadAll(r.Body)

return body
}

func getUrl(w http.ResponseWriter, _ *http.Request) {

body := GetBody(*yhost, *yport)

rj := map[string]string{}
json.Unmarshal(body, &rj)

pp := strings.Index(rj["url"], ":809")
p := strings.Index(rj["url"], "/if5ax")
t := strings.Index(rj["url"], "lsttm=")
ip := rj["url"][7:pp]

path := rj["url"][p:]
tt := rj["url"][t+6 : t+20]

vmess := []byte(`{"add":"` + ip + `","aid":"0","host":"` + ip + `","id":"` + *yuuid + `","net":"ws","path":"` + path + `","port":"809","ps":"联通809` + tt + `过期","sni":"","tls":"","type":"none","v":"2"}`)
vmessUrl := []byte("vmess://" + base64.StdEncoding.EncodeToString(vmess))
final := base64.StdEncoding.EncodeToString(vmessUrl)
fmt.Fprintf(w, final)


}


func main() {
flag.Parse()
if _, err := net.ResolveTCPAddr("tcp", *listen); err != nil {
log.Fatalf("解析监听地址异常,%s", err)
}
http.HandleFunc("/"+*route, getUrl)

log.Println("订阅地址监听", *listen+"/"+*route, "中...")
err := http.ListenAndServe(*listen, nil)
if err != nil {
log.Println("启动端口发生异常, 请检查端口是否被占用", err)
time.Sleep(time.Minute)
os.Exit(1)
}
}
使用方法

docker run --net host 740162752/unicom809 -help

总结

1、主要学习了简单的http-sever提供服务功能

2、base64加密解密

3、Golang 的交叉编译并通过docker快速部署项目

4、flag包的使用