-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
263 lines (244 loc) · 8.07 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"log"
"time"
// "io/ioutil"
// "reflect"
"net/http"
"encoding/base64"
// "crypto/aes"
// "crypto/sha256"
// "encoding/hex"
// json "encoding/json"
_ "net/http/pprof"
bcrypt "golang.org/x/crypto/bcrypt"
auth "github.com/abbot/go-http-auth"
//mjpeg "github.com/hybridgroup/mjpeg"
mjpeg "github.com/0187773933/RaspiCameraMotionTracker/v2/mjpeg"
motion "github.com/0187773933/RaspiCameraMotionTracker/v2/motion"
utils "github.com/0187773933/RaspiCameraMotionTracker/v2/utils"
jwt "github.com/dgrijalva/jwt-go"
securecookie "github.com/gorilla/securecookie"
)
// Switch to Fiber
// https://docs.gofiber.io/api/middleware/basicauth
// https://docs.gofiber.io/api/middleware#helmet
// https://docs.gofiber.io/api/app#handler
var stream *mjpeg.Stream
var JWT_SECRET = []byte("asdf")
var COOKIE_SECRET = []byte("asdf")
var COOKIE_SALT = []byte("asdf")
var COOKIE *securecookie.SecureCookie
var PASSWORD_SHA256_SUM = ""
// func Secret( user , realm string ) string {
// if user == "john" {
// // password is "hello"
// return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
// }
// return ""
// }
// https://github.com/abbot/go-http-auth/issues/56#issuecomment-393651189
// https://pkg.go.dev/golang.org/x/crypto/bcrypt#GenerateFromPassword
func Secret( user , realm string ) string {
if user == "admin" {
hashedPassword , err := bcrypt.GenerateFromPassword( []byte( "waduwaduwadu" ) , bcrypt.DefaultCost )
if err == nil {
return string( hashedPassword )
}
}
return ""
}
// https://golang.org/pkg/net/http/#ServeMux.HandleFunc
// https://github.com/abbot/go-http-auth/blob/7f557639efd97bd84723b69471931553e1e0ade9/basic.go#L134
// https://github.com/hybridgroup/mjpeg/blob/master/stream.go#L18
// https://golang.org/pkg/net/http/#ResponseWriter
func handle( w http.ResponseWriter , r *auth.AuthenticatedRequest ) {
w.Header().Add( "Content-Type" , "multipart/x-mixed-replace;boundary=MJPEGBOUNDARY" )
c := make( chan []byte )
stream.Lock.Lock()
stream.M[ c ] = true
stream.Lock.Unlock()
for {
time.Sleep( stream.FrameInterval )
b := <-c
_, err := w.Write( b )
if err != nil {
break
}
}
stream.Lock.Lock()
delete( stream.M , c )
stream.Lock.Unlock()
log.Println( "Stream:" , r.RemoteAddr , "disconnected" )
}
func isAuthorized( endpoint func( http.ResponseWriter , *http.Request ) ) http.Handler {
return http.HandlerFunc( func( w http.ResponseWriter , r *http.Request ) {
cookie_value := make(map[string]string)
if cookie , err := r.Cookie("rpmt-cookie"); err == nil {
if err = COOKIE.Decode("rpmt-cookie", cookie.Value, &cookie_value); err == nil {
fmt.Println( cookie_value )
token , err := jwt.Parse( cookie_value["token"] , func( token *jwt.Token ) ( interface{} , error ) {
fmt.Println( "here again" )
if _ , ok := token.Method.( *jwt.SigningMethodHMAC ); !ok {
fmt.Printf("There was an error")
}
return JWT_SECRET , nil
})
if err != nil { fmt.Fprintf(w, err.Error()) }
if token.Valid {
fmt.Println( token )
endpoint( w , r )
return
}
}
}
http.Redirect( w , r , "/" , http.StatusUnauthorized )
return
})
}
func GenerateJWT() (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["authorized"] = true
claims["client"] = "asdf"
claims["exp"] = time.Now().Add(time.Minute * 1036800).Unix()
tokenString, err := token.SignedString(JWT_SECRET)
if err != nil {
fmt.Errorf("Something Went Wrong: %s", err.Error())
return "", err
}
return tokenString, nil
}
type LoginResult struct {
Token string `json:"token"`
}
// func login( w http.ResponseWriter , r *auth.AuthenticatedRequest ) {
func gen_login_form( passphrase string ) ( login_form string ) {
login_form = fmt.Sprintf(`
<html>
<head>
<!-- https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js -->
<!-- https://codepen.io/gabrielizalo/pen/oLzaqx -->
<!-- <script src="https://39363.org/CDN/aes.js"></script> -->
<script src="https://39363.org/CDN/sha256.js"></script>
</head>
<body>
<form id="login_form" method="POST" action="/login" , onsubmit="post_login_form()" >
<input type="hidden" name="sha256sum">
Username : <input type="text" name="username">
<br>
Password : <input type="text" name="password">
<br>
<input type="submit" value="Login">
</form>
<script>
function post_login_form() {
let form = document.getElementById( "login_form" );
form.sha256sum.value = CryptoJS.SHA256( form.username.value + " === " + form.password.value ).toString();
form.username.value = "";
form.password.value = "";
return true;
}
</script>
</body>
</html>
`)
return
}
var LoginForm = ""
// func DecryptAES(key []byte, ct string) {
// ciphertext, _ := hex.DecodeString(ct)
// c, _ := aes.NewCipher(key)
// pt := make([]byte, len(ciphertext))
// c.Decrypt(pt, ciphertext)
// s := string(pt[:])
// fmt.Println("DECRYPTED:", s)
// }
func password_sums_match( test_sha256_sum string ) ( result bool ) {
result = false
// username_password_sha256_bytes := sha256.Sum256( []byte( "wadu === wadu" ) )
// username_password_sha256 := hex.EncodeToString( username_password_sha256_bytes[:] )
if test_sha256_sum == PASSWORD_SHA256_SUM {
result = true
}
return
}
func login( w http.ResponseWriter , r *http.Request ) {
switch r.Method {
case "GET":
w.Header().Set( "Content-Type" , "text/html; charset=utf-8" )
fmt.Fprint( w , LoginForm )
return
case "POST":
r.ParseForm()
test_sha256_sum := r.Form.Get("sha256sum")
if password_sums_match( test_sha256_sum ) == false {
w.Header().Set( "Content-Type" , "text/html; charset=utf-8" )
fmt.Fprint( w , LoginForm )
return
}
validToken , err := GenerateJWT()
if err != nil { fmt.Println("Failed to generate token") }
value := map[string]string{
"token": validToken ,
}
encoded , _ := COOKIE.Encode( "rpmt-cookie" , value );
cookie := &http.Cookie{
Name: "rpmt-cookie",
Value: encoded ,
Path: "/frame.jpeg" ,
// Path: "/test" ,
Secure: false ,
HttpOnly: false ,
}
fmt.Println( cookie )
http.SetCookie( w , cookie )
http.Redirect( w , r , "/frame.jpeg" , http.StatusTemporaryRedirect )
return
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
return
}
}
type TestAuthResult struct {
Result string `json:"result"`
}
func test_authenticated( w http.ResponseWriter , r *http.Request ) {
// w.Header().Set( "Content-Type" , "application/json" )
// data := TestAuthResult{}
// data.Result = "success"
// w.WriteHeader( http.StatusCreated )
// json.NewEncoder( w ).Encode( data )
// jsonResp , _ := json.Marshal( data )
// w.Write( jsonResp )
fmt.Println( "here???" )
}
func main() {
config := utils.ParseConfig()
JWT_SECRET = []byte( config.JWTSecret )
// COOKIE_SECRET = []byte( config.CookieSecret )
COOKIE_SECRET , _ = base64.StdEncoding.DecodeString( config.CookieSecret )
COOKIE_SALT , _ = base64.StdEncoding.DecodeString( config.CookieSalt )
COOKIE = securecookie.New( COOKIE_SECRET , COOKIE_SALT )
LoginForm = gen_login_form( config.LoginFormPassphrase )
PASSWORD_SHA256_SUM = config.LoginSHA256Sum
stream = mjpeg.NewStream( config.FrameIntervalMilliseconds , COOKIE , JWT_SECRET )
motion_tracker := motion.NewTracker( stream , &config )
// fmt.Println( motion_tracker )
go motion_tracker.Start()
// start http server
// https://medium.com/better-programming/hands-on-with-jwt-in-golang-8c986d1bb4c0
// http://localhost:9363/frame.jpeg
//http.Handle( "/frame.jpeg" , stream )
//authenticator := auth.NewBasicAuthenticator( "localhost" , Secret )
//http.HandleFunc( "/frame.jpeg" , authenticator.Wrap( handle ) )
//http.HandleFunc( "/frame.jpeg" , handle )
// log.Fatal( http.ListenAndServe( "0.0.0.0:9767" , nil ) )
// somehow it never calls ServeHTTP() ??? http.Handle implicitly calls it ???
// https://tutorialedge.net/golang/authenticating-golang-rest-api-with-jwts/
http.Handle( config.ServerMJPEGEndpointURL , stream )
http.HandleFunc( "/login" , login )
http.Handle( "/test" , isAuthorized( test_authenticated ) )
http.ListenAndServe( fmt.Sprintf( "0.0.0.0:%s" , config.ServerPort ) , nil )
}