-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgobang.go
175 lines (167 loc) · 5.05 KB
/
gobang.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"crypto/sha1"
"fmt"
"github.com/bitly/go-simplejson"
"github.com/hcrgm/Gobang-Go/gobang"
"github.com/kataras/go-sessions"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/gommon/random"
"golang.org/x/net/html"
"html/template"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
type Template struct {
templates *template.Template
}
type Config struct {
debug bool
port int
useOAuth bool
github *OAuthConfig
}
type OAuthConfig struct {
client_id string
client_secret string
}
var config *Config
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
configFile, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
json, err := simplejson.NewJson(configFile)
if err != nil {
panic(err)
}
config = &Config{
debug: json.Get("debug").MustBool(false),
port: json.Get("port").MustInt(8011),
useOAuth: json.Get("useOAuth").MustBool(false),
github: &OAuthConfig{
client_id: json.GetPath("github").Get("client_id").MustString(""),
client_secret: json.GetPath("github").Get("client_secret").MustString(""),
},
}
if config.useOAuth {
if len(config.github.client_id) == 0 || len(config.github.client_secret) == 0 {
panic("Wrong config:Github OAuth")
}
}
e := echo.New()
// debug
e.Debug = config.debug
e.Use(middleware.Gzip())
e.Static("/", "public")
if config.debug {
e.Use(middleware.Logger())
}
t := &Template{
templates: template.Must(template.New("").Funcs(template.FuncMap{
"loop": func(n int) []int {
slice := make([]int, n)
for i := 0; i < n; i++ {
slice[i] = i + 1
}
return slice
},
}).ParseGlob("template/*.html")),
}
e.Renderer = t
e.GET("/status", gobang.HandleStatusSocket)
e.GET("/socket", gobang.HandleGameSocket)
e.GET("/game", gobang.Game)
e.Any("/login", Login)
e.Logger.Fatal(e.Start(":" + strconv.Itoa(config.port)))
}
func Login(c echo.Context) error {
if !config.useOAuth {
return c.HTML(http.StatusOK, `[<b style="color:#f44336">Login function not enabled</b>]`)
}
response := ""
w := c.Response().Writer
r := c.Request()
sess := sessions.Start(w, r)
switch c.FormValue("action") {
case "logout":
sess.Delete("name")
fallthrough
case "info":
username := "Anonymous"
signBtn := "Sign in"
if name := sess.GetString("name"); len(name) != 0 {
username = name
signBtn = "Sign out"
}
response = fmt.Sprintf(`<span id="username">%s</span> <a id="btn_login">[%s]</a><script>$("#btn_login").one("click", login);</script>`, username, signBtn)
case "oauth":
sha := sha1.New()
sha.Write([]byte(random.String(16)))
state := fmt.Sprintf("%x", sha.Sum(nil))
sess.Set("state", state)
return c.Redirect(http.StatusFound, "https://github.com/login/oauth/authorize?client_id="+config.github.client_id+"&state="+state)
case "oauth-callback":
if len(c.FormValue("code")) == 0 || len(c.FormValue("state")) == 0 || sess.GetString("state") != c.FormValue("state") {
return c.HTML(http.StatusBadRequest, "Bad Request")
}
if code := c.FormValue("code"); len("code") != 0 {
v := url.Values{}
v.Set("client_id", config.github.client_id)
v.Set("client_secret", config.github.client_secret)
v.Set("code", code)
req, err := http.NewRequest("POST", "https://github.com/login/oauth/access_token", strings.NewReader(v.Encode()))
if err != nil {
return c.HTML(http.StatusInternalServerError, err.Error())
}
req.Header.Add("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := http.DefaultClient.Do(req)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Cannot send the request to GitHub")
}
json, err := simplejson.NewFromReader(response.Body)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Cannot parse the result")
}
accessToken := json.Get("access_token").MustString("")
if len(accessToken) == 0 {
return c.HTML(http.StatusInternalServerError, "Cannot get the access token")
}
v = url.Values{}
v.Set("access_token", accessToken)
req, err = http.NewRequest("GET", "https://api.github.com/user", nil)
if err != nil {
return c.HTML(http.StatusInternalServerError, err.Error())
}
req.Header.Set("Authorization", "token "+accessToken)
response, err = http.DefaultClient.Do(req)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Cannot send the request to GitHub")
}
json, err = simplejson.NewFromReader(response.Body)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Cannot parse the result")
}
name := json.Get("name").MustString("")
if len(name) == 0 {
name = json.Get("login").MustString("")
if len(name) == 0 {
return c.HTML(http.StatusInternalServerError, "Cannot get the name")
}
}
name = html.EscapeString(name)
sess.Set("name", name)
return c.Redirect(http.StatusFound, "index.html")
}
}
return c.HTML(http.StatusOK, response)
}