token-validator: use SuffixIP, can modify it and can delete tunnels
This commit is contained in:
parent
060831d9c2
commit
da1920673d
4 changed files with 137 additions and 9 deletions
|
|
@ -33,6 +33,8 @@ func init() {
|
|||
router.POST("/api/wg/", apiAuthHandler(genWgToken))
|
||||
router.GET("/api/wg/:token", getWgTunnelInfo)
|
||||
router.POST("/api/wg/:token", getWgTunnelInfo)
|
||||
router.PUT("/api/wg/:token", apiAuthHandler(updateWgTunnel))
|
||||
router.DELETE("/api/wg/:token", apiAuthHandler(deleteWgTunnel))
|
||||
}
|
||||
|
||||
func showWgTunnel(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
|
|
@ -121,6 +123,11 @@ func getWgTunnelInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Param
|
|||
return
|
||||
}
|
||||
|
||||
// 0 is considered default for suffix, apply default now
|
||||
if token.SuffixIP <= 0 {
|
||||
token.SuffixIP = 1
|
||||
}
|
||||
|
||||
syncWgConf()
|
||||
|
||||
tinfo := getTunnelInfo(token.IdStudent)
|
||||
|
|
@ -138,11 +145,58 @@ PublicKey = %s
|
|||
Endpoint = %s:%d
|
||||
AllowedIPs = ::/0
|
||||
PersistentKeepalive = 5
|
||||
# MyIPv6=%s1/%d
|
||||
# MyIPv6=%s%x/%d
|
||||
# MyNetwork=%s/%d
|
||||
# GWIPv6=%s
|
||||
# MyLogin=%s
|
||||
`, base64.StdEncoding.EncodeToString(tinfo.SrvPubKey), "82.64.31.248", tinfo.SrvPort, tinfo.CltIPv6, 64, tinfo.CltIPv6, tinfo.CltRange, tinfo.SrvGW6, student.Login)))
|
||||
`, base64.StdEncoding.EncodeToString(tinfo.SrvPubKey), "82.64.31.248", tinfo.SrvPort, tinfo.CltIPv6, token.SuffixIP, 64, tinfo.CltIPv6, tinfo.CltRange, tinfo.SrvGW6, student.Login)))
|
||||
}
|
||||
|
||||
func updateWgTunnel(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
token, err := adlin.GetTunnelToken(adlin.TokenFromText(ps.ByName("token")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if token.IdStudent != student.Id {
|
||||
return nil, fmt.Errorf("Unauthorized")
|
||||
}
|
||||
|
||||
var newToken adlin.TunnelToken
|
||||
if err := json.Unmarshal(body, &newToken); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token.TokenText = newToken.TokenText
|
||||
token.PubKey = newToken.PubKey
|
||||
token.SuffixIP = newToken.SuffixIP
|
||||
|
||||
if _, err = token.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
syncWgConf()
|
||||
|
||||
return true, err
|
||||
}
|
||||
|
||||
func deleteWgTunnel(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
token, err := adlin.GetTunnelToken(adlin.TokenFromText(ps.ByName("token")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if token.IdStudent != student.Id {
|
||||
return nil, fmt.Errorf("Unauthorized")
|
||||
}
|
||||
|
||||
if _, err = token.Delete(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
syncWgConf()
|
||||
|
||||
return true, err
|
||||
}
|
||||
|
||||
func GenWGConfig(w io.Writer) error {
|
||||
|
|
|
|||
Reference in a new issue