forked from genshen/ssh-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed beego framework and added jwt authenticate
- Loading branch information
Showing
26 changed files
with
700 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,6 @@ | |
.glide/ | ||
|
||
*.exe~ | ||
dist/ | ||
temp/ | ||
static/ | ||
views/ | ||
.idea/ | ||
.idea/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.yaml |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
site: | ||
appname: webConsole | ||
listen_addr: :2222 | ||
runmode: prod | ||
static_dir: static/ | ||
views_dir: views/ | ||
ssh: | ||
io_mode: 1 # the mode reading data from ssh server: channel mode (0) OR session mode (1) | ||
jwt: | ||
jwt_secret: secret.console.hpc.gensh.me | ||
token_lifetime: 7200 | ||
issuer: issuer.ssh.gensh.me | ||
query_token_key: _t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
site: | ||
appname: webConsole | ||
listen_addr: :2222 | ||
runmode: prod | ||
static_dir: /static/ | ||
ssh: | ||
io_mode: 1 # the mode reading data from ssh server: channel mode (0) OR session mode (1) | ||
jwt: | ||
jwt_secret: secret.console.hpc.gensh.me | ||
token_lifetime: 7200 | ||
issuer: issuer.ssh.gensh.me |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package controllers | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
"net/http" | ||
"github.com/genshen/webConsole/src/utils" | ||
) | ||
|
||
type AfterAuthenticated interface { | ||
// make sure token and session is not nil. | ||
ServeAfterAuthenticated(w http.ResponseWriter, r *http.Request, token *utils.Claims, session *utils.Session) | ||
} | ||
|
||
func AuthPreChecker(i AfterAuthenticated) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var authHead = r.Header.Get("Authorization") | ||
var token string | ||
if authHead != "" { | ||
lIndex := strings.LastIndex(authHead, " ") | ||
if lIndex < 0 || lIndex+1 >= len(authHead) { | ||
utils.Abort(w, "invalid token", 400) | ||
log.Println("Error: invalid token", 400) | ||
return | ||
} else { | ||
token = authHead[lIndex+1:] | ||
} | ||
} else { | ||
if token = r.URL.Query().Get(utils.Config.Jwt.QueryTokenKey); token == "" { | ||
utils.Abort(w, "invalid token", 400) | ||
log.Println("Error: invalid token", 400) | ||
return | ||
} // else token != "", then passed and go on running | ||
} | ||
|
||
if claims, err := utils.JwtVerify(token); err != nil { | ||
http.Error(w, "invalid token", 400) | ||
log.Println("Error: Cannot setup WebSocket connection:", err) | ||
} else { // check passed. | ||
// check session. | ||
if session, ok := utils.SessionStorage.Get(token); !ok { | ||
utils.Abort(w, "Error: Cannot get Session data:", 400) | ||
log.Println("Error: Cannot get Session data for token", token) | ||
} else { | ||
defer utils.SessionStorage.Delete(token) // clear session after ssh closed. | ||
i.ServeAfterAuthenticated(w, r, claims, &session) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,63 @@ | ||
package controllers | ||
|
||
import ( | ||
"strconv" | ||
"net/http" | ||
"github.com/genshen/webConsole/src/models" | ||
"github.com/genshen/webConsole/src/utils" | ||
) | ||
|
||
type MainController struct { | ||
BaseController | ||
func Get(w http.ResponseWriter, r *http.Request) { | ||
utils.ServeHTTPByName(w,r,"index.html") | ||
} | ||
|
||
func (this *MainController) Get() { | ||
this.TplName = "index.html" | ||
} | ||
|
||
const ( | ||
SIGN_IN_FORM_TYPE_ERROR_VALID = iota | ||
SIGN_IN_FORM_TYPE_ERROR_PASSWORD | ||
SIGN_IN_FORM_TYPE_ERROR_TEST | ||
) | ||
func SignIn(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "POST" { | ||
http.Error(w, "Invalid request method.", 405) | ||
} else { | ||
var err error | ||
var errUnmarshal models.SignInFormValid | ||
err = r.ParseForm() | ||
if err != nil { | ||
panic(err) | ||
} | ||
userinfo := models.UserInfo{} | ||
userinfo.Host = r.Form.Get("host") | ||
port := r.Form.Get("port") | ||
userinfo.Username = r.Form.Get("username") | ||
userinfo.Password = r.Form.Get("passwd") | ||
|
||
func (this *MainController) SignIn() { | ||
var err error; | ||
userinfo := models.UserInfo{} | ||
userinfo.Host = this.GetString("host") | ||
userinfo.Port, err = this.GetInt("port", 22) | ||
userinfo.Username = this.GetString("username") | ||
userinfo.Password = this.GetString("passwd") | ||
if err == nil && userinfo.Host != "" && userinfo.Username != "" { | ||
//try to login ssh account | ||
ssh := utils.SSH{} | ||
ssh.Node.Host = userinfo.Host | ||
ssh.Node.Port = userinfo.Port | ||
_, err := ssh.Connect(userinfo.Username, userinfo.Password) | ||
userinfo.Port, err = strconv.Atoi(port) | ||
if err != nil { | ||
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_PASSWORD} | ||
this.Data["json"] = &errUnmarshal | ||
} else { | ||
defer ssh.Close() | ||
// create session | ||
if session, err := ssh.Client.NewSession(); err == nil { | ||
if err := session.Run("whoami"); err == nil { | ||
this.SetSession("userinfo", userinfo) | ||
errUnmarshal := models.SignInFormValid{HasError: false} | ||
this.Data["json"] = &errUnmarshal | ||
this.ServeJSON() | ||
return | ||
userinfo.Port = 22 | ||
} | ||
|
||
if userinfo.Host != "" && userinfo.Username != "" { | ||
//try to login ssh account | ||
ssh := utils.SSH{} | ||
ssh.Node.Host = userinfo.Host | ||
ssh.Node.Port = userinfo.Port | ||
_, err := ssh.Connect(userinfo.Username, userinfo.Password) | ||
if err != nil { | ||
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_PASSWORD} | ||
} else { | ||
defer ssh.Close() | ||
// create session | ||
if session, err := ssh.Client.NewSession(); err == nil { | ||
if err := session.Run("whoami"); err == nil { | ||
if token, expireUnix, err := utils.JwtNewToken(userinfo.Connection, utils.Config.Jwt.Issuer); err == nil { | ||
errUnmarshal = models.SignInFormValid{HasError: false, Addition: token} | ||
utils.ServeJSON(w, errUnmarshal) | ||
utils.SessionStorage.Put(token, expireUnix, userinfo) | ||
return | ||
} | ||
} | ||
} | ||
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_TEST} | ||
} | ||
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_TEST} | ||
this.Data["json"] = &errUnmarshal | ||
} else { | ||
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_VALID} | ||
} | ||
} else { | ||
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_VALID} | ||
this.Data["json"] = &errUnmarshal | ||
utils.ServeJSON(w, errUnmarshal) | ||
} | ||
this.ServeJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/astaxie/beego" | ||
"github.com/genshen/webConsole/src/models" | ||
"log" | ||
"net/http" | ||
"github.com/genshen/webConsole/src/utils" | ||
"github.com/genshen/webConsole/src/models" | ||
) | ||
|
||
type UploadController struct { | ||
BaseController | ||
} | ||
type FileUpload struct{} | ||
|
||
func (this *UploadController) UploadFile() { | ||
file, header, err := this.GetFile("file") | ||
func (f FileUpload) ServeAfterAuthenticated(w http.ResponseWriter, r *http.Request, claims *utils.Claims, session *utils.Session) { | ||
//file, header, err := this.GetFile("file") | ||
|
||
r.ParseMultipartForm(32 << 20) | ||
file, header, err := r.FormFile("file") | ||
if err != nil { | ||
beego.Error("getfile err ", err) | ||
this.Abort("503") | ||
log.Println("Error: getfile err ", err) | ||
utils.Abort(w, "error", 503) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
user := session.Value.(models.UserInfo) | ||
if err := utils.UploadFile(utils.SftpNode{Host: user.Host, Port: user.Port}, user.Username, user.Password, file, header); err != nil { | ||
log.Println("Error: sftp error:", err) | ||
utils.Abort(w, "message", 503) | ||
} else { | ||
v := this.GetSession("userinfo") | ||
if v == nil { | ||
beego.Error("Cannot get Session data:", err) | ||
this.Abort("503") | ||
} else { | ||
user := v.(models.UserInfo) | ||
if err := utils.UploadFile(user, file, header); err != nil { | ||
beego.Error("sftp error:", err) | ||
this.Abort("503") | ||
} else { | ||
this.Ctx.WriteString("sussess") | ||
} | ||
} | ||
w.Write([]byte("sussess")) | ||
} | ||
} |
Oops, something went wrong.