Skip to content

Commit

Permalink
Migrate to go-gin (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
forewing authored Dec 24, 2020
1 parent 27e3ee3 commit 3e61727
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 137 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ A web based control panel for srcds' RCON protocol (CS:GO).
```
Usage of webrcon-server:
-addr address
address of the server RCON, in the format of HOST:PORT. (default "127.0.0.1:27015")
address of the server RCON, in the format of HOST:PORT (default "127.0.0.1:27015")
-admin-name username
basicauth username for path /api/exec
-admin-pass password
basicauth password for path /api/exec
-bind address
webrcon-server bind address (default "0.0.0.0:8080")
-conf file
load configs from file instead of flags.
load configs from file instead of flags
-debug
turn on debug mode
-pass password
password of the RCON.
password of the RCON
-timeout timeout
timeout of the connection (seconds). (default 1)
timeout(seconds) of the connection (default 1)
```

### API
Expand All @@ -50,7 +52,7 @@ Browse to `http://127.0.0.1:8080/`
Please help us to add more command shortcut.
In resources/main.js, edit the object
In statics/main.js, edit the object
```
shortcutGroups: {
Expand Down
63 changes: 63 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

func getConnect(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("steam://connect/%v", *flags.Address))
}

func getExec(c *gin.Context) {
cmd := c.DefaultQuery("cmd", "")
msg, err := exec(cmd)
if err != nil {
c.String(http.StatusBadRequest, "%v\n%v", msg, err)
return
}

c.String(http.StatusOK, msg)
}

type execModel struct {
Cmd string `json:"cmd" binding:"required"`
}

func postExec(c *gin.Context) {
var postData execModel
if err := c.ShouldBindJSON(&postData); err != nil {
c.String(http.StatusBadRequest, "%v", err)
return
}
cmd := postData.Cmd

msg, err := exec(cmd)
if err != nil {
c.String(http.StatusBadRequest, "%v\n%v", msg, err)
return
}
c.String(http.StatusOK, msg)
}

func exec(cmd string) (string, error) {
cmd = strings.TrimSpace(cmd)
if len(cmd) == 0 {
return "Error, empty command", nil
}

log.Println("cmd: ", cmd)
msg, err := client.Execute(cmd)
msg = strings.TrimSpace(msg)
log.Println("msg: ", msg)

if err != nil {
log.Println("err: ", err)
}

return msg, err
}
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"Bind": "0.0.0.0:8080",
"BasicAuthUsername": "admin",
"BasicAuthPassword": "password",
"BasicAuthDisabled": false
"Debug": false
}
17 changes: 8 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ type Flags struct {
Bind *string `json:",omitempty"`
BasicAuthUsername *string `json:",omitempty"`
BasicAuthPassword *string `json:",omitempty"`
BasicAuthDisabled bool `json:",omitempty"`

Debug *bool `json:",omitempty"`
}

var (
flags Flags = Flags{
Address: flag.String("addr", rcon.DefaultAddress, "`address` of the server RCON, in the format of HOST:PORT."),
Password: flag.String("pass", rcon.DefaultPassword, "`password` of the RCON."),
Timeout: flag.Float64("timeout", rcon.DefaultTimeout.Seconds(), "`timeout` of the connection (seconds)."),
Address: flag.String("addr", rcon.DefaultAddress, "`address` of the server RCON, in the format of HOST:PORT"),
Password: flag.String("pass", rcon.DefaultPassword, "`password` of the RCON"),
Timeout: flag.Float64("timeout", rcon.DefaultTimeout.Seconds(), "`timeout`(seconds) of the connection"),

Config: flag.String("conf", "", "load configs from `file` instead of flags."),
Config: flag.String("conf", "", "load configs from `file` instead of flags"),

Bind: flag.String("bind", "0.0.0.0:8080", "webrcon-server bind `address`"),
BasicAuthUsername: flag.String("admin-name", "", "basicauth `username` for path /api/exec"),
BasicAuthPassword: flag.String("admin-pass", "", "basicauth `password` for path /api/exec"),

Debug: flag.Bool("debug", false, "turn on debug mode"),
}
)

Expand All @@ -49,8 +52,4 @@ func init() {
if err != nil {
panic(err)
}

if len(*flags.BasicAuthUsername) == 0 && len(*flags.BasicAuthPassword) == 0 {
flags.BasicAuthDisabled = true
}
}
33 changes: 18 additions & 15 deletions generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,42 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/go-bindata/go-bindata/v3"
)

const (
output = "./bindata.go"
output = "./bindata.go"
ignoreFile = `.*\.go`
)

var (
config = bindata.Config{
Package: "main",
Output: output,
func main() {
generate("./statics")
generate("./templates")
}

Prefix: "resources/",
func generate(path string) {
cleanPath := filepath.Clean(path)
config := bindata.Config{
Package: cleanPath,
Output: filepath.Join(cleanPath, output),
Prefix: cleanPath + "/",
Input: []bindata.InputConfig{
bindata.InputConfig{
Path: filepath.Clean("resources/"),
Path: cleanPath,
Recursive: false,
},
},

Ignore: []*regexp.Regexp{
regexp.MustCompile(ignoreFile),
},
HttpFileSystem: true,
}
)

func main() {
generate()
}

func generate() {
err := bindata.Translate(&config)
if err != nil {
fmt.Fprintf(os.Stderr, "bindata: %v\n", err)
fmt.Fprintf(os.Stderr, "bindata, %v: %v\n", path, err)
os.Exit(1)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module webrcon-server

go 1.14
go 1.15

require (
github.com/forewing/csgo-rcon v1.1.0
github.com/gin-gonic/gin v1.6.3
github.com/go-bindata/go-bindata/v3 v3.1.3
)
47 changes: 47 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/forewing/csgo-rcon v1.1.0 h1:dKubPVEs1WhICF4rjhDEuJqW+TwhaL/0ckh5u5Cy9e8=
github.com/forewing/csgo-rcon v1.1.0/go.mod h1:E6Mo02QwOx2zqxd2EmKgLUlxxjRWAobC3P7nrkXb3rk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-bindata/go-bindata/v3 v3.1.3 h1:F0nVttLC3ws0ojc7p60veTurcOm//D4QBODNM7EGrCI=
github.com/go-bindata/go-bindata/v3 v3.1.3/go.mod h1:1/zrpXsLD8YDIbhZRqXzm1Ghc7NhEvIN9+Z6R5/xH4I=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f h1:kDxGY2VmgABOe55qheT/TFqUMtcTHnomIPS1iv3G4Ms=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 3e61727

Please sign in to comment.