forked from zhixiangjoy/v2ary2clash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
91 lines (79 loc) · 2.13 KB
/
main.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"clashconfig/api"
"clashconfig/middleware"
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
)
var (
listenAddr string
listenPort string
h bool
)
func init() {
flag.BoolVar(&h, "h", false, "help")
flag.StringVar(&listenAddr, "l", "0.0.0.0", "listen address")
flag.StringVar(&listenPort, "p", "5050", "listen port")
flag.Parse()
}
func DownLoadTemplate(url string, path string) {
log.Printf("Rule template URL: %s", url)
log.Println("Start downloading the rules template")
resp, err := http.Get(url)
if nil != err {
log.Fatalf("Rule template download failed, please manually download save as [%s]\n", path)
}
defer resp.Body.Close()
s, err := ioutil.ReadAll(resp.Body)
if nil != err || resp.StatusCode != http.StatusOK {
log.Fatalf("Rule template download failed, please manually download save as [%s]\n", path)
}
ioutil.WriteFile(path, s, 0777)
log.Printf("Rules template download complete. [%s]\n", path)
}
func main() {
if h {
flag.Usage()
return
}
_, err := os.Stat("ConnersHua.yaml")
if err != nil && os.IsNotExist(err) {
DownLoadTemplate("https://raw.githubusercontent.com/ConnersHua/Profiles/master/Clash/Pro.yaml", "ConnersHua.yaml")
}
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(middleware.PreMiddleware())
router.GET("/v2ray2clash", api.V2ray2Clash)
router.GET("/v2ray2quanx", api.V2ray2Quanx)
router.GET("/ssr2clashr", api.SSR2ClashR)
srv := &http.Server{
Addr: fmt.Sprintf("%s:%s", listenAddr, listenPort),
Handler: router,
}
go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}