diff --git a/README.md b/README.md new file mode 100644 index 0000000..730bafe --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# DDatHome-go +DD@Home in golang + +#### 直接运行 +``` +./DDatHome +``` + +#### 作为系统服务安装 +支持Windows和Linux,需要管理员权限,服务名:DDatHome +``` +./DDatHome install +``` +卸载服务 +``` +./DDatHome uninstall +``` + +#### 配置文件 +``` +{ + "nickname":"DD", //这里是昵称 + "interval":500 //这里是任务处理间隔(单位: ms) +} +``` diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..a4e978a --- /dev/null +++ b/build.bat @@ -0,0 +1,33 @@ +@ECHO OFF +mkdir bin + +::windows x64 +set GOOS=windows +set GOARCH=amd64 +go build -ldflags "-s -w" -o .\bin\DDatHome-windows-amd64.exe + +::windows x32 +set GOOS=windows +set GOARCH=386 +go build -ldflags "-s -w" -o .\bin\DDatHome-windows-386.exe + +::linux x64 +set GOOS=linux +set GOARCH=amd64 +go build -ldflags "-s -w" -o .\bin\DDatHome-linux-amd64 + +::linux x32 +set GOOS=linux +set GOARCH=386 +go build -ldflags "-s -w" -o .\bin\DDatHome-linux-386 + +::linux arm7 +set GOOS=linux +set GOARCH=arm +set GOARM=7 +go build -ldflags "-s -w" -o .\bin\DDatHome-linux-arm7 + +::linux arm64 +set GOOS=linux +set GOARCH=arm64 +go build -ldflags "-s -w" -o .\bin\DDatHome-linux-arm64 diff --git a/config.json b/config.json new file mode 100644 index 0000000..ba80f30 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "nickname":"DD", + "interval":500 +} \ No newline at end of file diff --git a/go.mod b/go.mod index 14ca780..b227e5f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module DD-Go go 1.13 require ( - github.com/tidwall/gjson v1.3.2 - golang.org/x/net v0.0.0-20190923162816-aa69164e4478 + github.com/kardianos/service v1.0.0 + github.com/stretchr/testify v1.5.1 // indirect + github.com/tidwall/gjson v1.6.0 + golang.org/x/net v0.0.0-20200513185701-a91f0712d120 ) diff --git a/go.sum b/go.sum index fb3816f..bd62a81 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,27 @@ -github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI= -github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kardianos/service v1.0.0 h1:HgQS3mFfOlyntWX8Oke98JcJLqt1DBcHR4kxShpYef0= +github.com/kardianos/service v1.0.0/go.mod h1:8CzDhVuCuugtsHyZoTvsOBuvonN/UDBvl0kH+BUxvbo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc= +github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY= +golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 0ca1f76..a1a4448 100644 --- a/main.go +++ b/main.go @@ -6,16 +6,26 @@ import ( "encoding/json" "errors" "fmt" + "github.com/kardianos/service" "github.com/tidwall/gjson" "golang.org/x/net/websocket" "io/ioutil" "net/http" + "net/url" "os" + "path/filepath" "runtime" "strings" "time" ) +type program struct{} + +func (p *program) Start(s service.Service) error { + go p.run() + return nil +} + type GoResult struct { Key string `json:"key"` Data string `json:"data"` @@ -23,18 +33,29 @@ type GoResult struct { } var ( - ws *websocket.Conn + ddName string = "DD" + interval float64 = 500 + version string = "1.0.0" + ws *websocket.Conn ) -func main() { - name := "dd-go" - if len(os.Args) > 1 { - name = strings.Join(os.Args[1:], " ") +func (p *program) run() { + FileName := getCurrentDirectory() + "/config.json" + if Exists(FileName) { + b, err := ioutil.ReadFile(FileName) + if err != nil { + panic(err) + } + jsons := gjson.Parse(string(b)) + ddName = jsons.Get("nickname").Str + interval = jsons.Get("interval").Num } - url := "wss://cluster.vtbs.moe/?runtime=" + runtime.Version() + "&version=0.3&platform=" + runtime.GOOS + "@" + runtime.GOARCH + "&name=" + name - fmt.Println("Dial", url) + + urls := "wss://cluster.vtbs.moe/?runtime=go&version=" + version + "&platform=" + runtime.GOOS + "&name=" + url.QueryEscape(ddName) + + fmt.Println("Dial", urls) connect := func() error { - conn, err := websocket.Dial(url, "", "https://cluster.vtbs.moe") + conn, err := websocket.Dial(urls, "", "https://cluster.vtbs.moe") if err != nil { return err } @@ -45,7 +66,7 @@ func main() { panic(err) } for { - time.Sleep(time.Millisecond * 500) + time.Sleep(time.Millisecond * time.Duration(interval)) _, err := ws.Write([]byte("DDhttp")) if err != nil { _ = ws.Close() @@ -74,6 +95,54 @@ func main() { } } +func (p *program) Stop(s service.Service) error { + return nil +} + +func main() { + svcConfig := &service.Config{ + Name: "DDatHome", + DisplayName: "DD@Home", + Description: "DD@home Service", + } + prg := &program{} + s, err := service.New(prg, svcConfig) + if err != nil { + fmt.Println(err) + } + + if err != nil { + fmt.Println(err) + } + + if len(os.Args) > 1 { + if os.Args[1] == "install" { + err := s.Install() + if err != nil { + fmt.Println("Service install failed: " + err.Error()) + return + } + fmt.Println("Service install successfully!") + return + } + + if os.Args[1] == "uninstall" { + err := s.Uninstall() + if err != nil { + fmt.Println("Service uninstall failed" + err.Error()) + return + } + fmt.Println("Service uninstall successfully!") + return + } + } + + err = s.Run() + if err != nil { + fmt.Println(err) + } +} + func Processor(payload []byte) (string, string, error) { json := gjson.Parse(string(payload)) key := json.Get("key").Str @@ -86,7 +155,7 @@ func Processor(payload []byte) (string, string, error) { fmt.Println("task", key, "error:", err) return "", key, err } - fmt.Println("task", key, "handled, url:", json.Get("data.url").Str) + //fmt.Println("task", key, "handled, url:", json.Get("data.url").Str) return data, key, nil } @@ -120,3 +189,22 @@ func GetString(url string) (string, error) { } return string(bytes), nil } + +func getCurrentDirectory() string { + dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + panic(err) + } + return strings.Replace(dir, "\\", "/", -1) +} + +func Exists(path string) bool { + _, err := os.Stat(path) + if err != nil { + if os.IsExist(err) { + return true + } + return false + } + return true +}