- 修改接入资产时窗口标题为资产名称
- 增加用户属性表
This commit is contained in:
parent
48b978b2c2
commit
86ef89ff21
@ -364,7 +364,7 @@ func SessionLsEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tun.Subject.SftpClient == nil {
|
if tun.Subject.SftpClient == nil {
|
||||||
sftpClient, err := CreateSftpClient(session.AssetId)
|
sftpClient, err := CreateSftpClient(session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("创建sftp客户端失败:%v", err.Error())
|
logrus.Errorf("创建sftp客户端失败:%v", err.Error())
|
||||||
return err
|
return err
|
||||||
|
@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@ -73,10 +72,20 @@ func SSHEndpoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
assetId := c.QueryParam("assetId")
|
sessionId := c.QueryParam("sessionId")
|
||||||
width, _ := strconv.Atoi(c.QueryParam("width"))
|
width, _ := strconv.Atoi(c.QueryParam("width"))
|
||||||
height, _ := strconv.Atoi(c.QueryParam("height"))
|
height, _ := strconv.Atoi(c.QueryParam("height"))
|
||||||
|
|
||||||
|
aSession, err := model.FindSessionById(sessionId)
|
||||||
|
if err != nil {
|
||||||
|
msg := Message{
|
||||||
|
Type: Closed,
|
||||||
|
Content: "get session error." + err.Error(),
|
||||||
|
}
|
||||||
|
_ = WriteMessage(ws, msg)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
user, _ := GetCurrentAccount(c)
|
user, _ := GetCurrentAccount(c)
|
||||||
if model.TypeUser == user.Type {
|
if model.TypeUser == user.Type {
|
||||||
// 检测是否有访问权限
|
// 检测是否有访问权限
|
||||||
@ -85,12 +94,16 @@ func SSHEndpoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !utils.Contains(assetIds, assetId) {
|
if !utils.Contains(assetIds, aSession.AssetId) {
|
||||||
return errors.New("您没有权限访问此资产")
|
msg := Message{
|
||||||
|
Type: Closed,
|
||||||
|
Content: "您没有权限访问此资产",
|
||||||
|
}
|
||||||
|
return WriteMessage(ws, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sshClient, err := CreateSshClient(assetId)
|
sshClient, err := CreateSshClientBySession(aSession)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("创建SSH客户端失败:%v", err.Error())
|
logrus.Errorf("创建SSH客户端失败:%v", err.Error())
|
||||||
msg := Message{
|
msg := Message{
|
||||||
@ -142,7 +155,7 @@ func SSHEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
_ = WriteMessage(ws, msg)
|
_ = WriteMessage(ws, msg)
|
||||||
|
|
||||||
recorder, err := NewRecorder("./" + assetId + ".cast")
|
recorder, err := NewRecorder("./" + sessionId + ".cast")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -179,13 +192,12 @@ func SSHEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
s := string(p)
|
s := string(p)
|
||||||
|
// 录屏
|
||||||
|
_ = recorder.WriteData(s)
|
||||||
msg := Message{
|
msg := Message{
|
||||||
Type: Data,
|
Type: Data,
|
||||||
Content: s,
|
Content: s,
|
||||||
}
|
}
|
||||||
// 录屏
|
|
||||||
_ = recorder.WriteData(s)
|
|
||||||
|
|
||||||
message, err := json.Marshal(msg)
|
message, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnf("生成Json失败 %v", err)
|
logrus.Warnf("生成Json失败 %v", err)
|
||||||
@ -231,6 +243,11 @@ func SSHEndpoint(c echo.Context) error {
|
|||||||
_, err = stdinPipe.Write([]byte(msg.Content))
|
_, err = stdinPipe.Write([]byte(msg.Content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugf("SSH会话写入失败: %v", err)
|
logrus.Debugf("SSH会话写入失败: %v", err)
|
||||||
|
msg := Message{
|
||||||
|
Type: Closed,
|
||||||
|
Content: "the remote connection is closed.",
|
||||||
|
}
|
||||||
|
_ = WriteMessage(ws, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,36 +264,17 @@ func WriteMessage(ws *websocket.Conn, msg Message) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSshClient(assetId string) (*ssh.Client, error) {
|
func CreateSshClientBySession(session model.Session) (sshClient *ssh.Client, err error) {
|
||||||
asset, err := model.FindAssetById(assetId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
accountType = asset.AccountType
|
username = session.Username
|
||||||
username = asset.Username
|
password = session.Password
|
||||||
password = asset.Password
|
privateKey = session.PrivateKey
|
||||||
privateKey = asset.PrivateKey
|
passphrase = session.Passphrase
|
||||||
passphrase = asset.Passphrase
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var authMethod ssh.AuthMethod
|
var authMethod ssh.AuthMethod
|
||||||
if accountType == "credential" {
|
if username == "-" || username == "" {
|
||||||
|
username = "root"
|
||||||
credential, err := model.FindCredentialById(asset.CredentialId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
accountType = credential.Type
|
|
||||||
username = credential.Username
|
|
||||||
password = credential.Password
|
|
||||||
privateKey = credential.PrivateKey
|
|
||||||
passphrase = credential.Passphrase
|
|
||||||
}
|
|
||||||
|
|
||||||
if username == "-" {
|
|
||||||
username = ""
|
|
||||||
}
|
}
|
||||||
if password == "-" {
|
if password == "-" {
|
||||||
password = ""
|
password = ""
|
||||||
@ -288,7 +286,7 @@ func CreateSshClient(assetId string) (*ssh.Client, error) {
|
|||||||
passphrase = ""
|
passphrase = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if accountType == model.PrivateKey {
|
if privateKey != "" {
|
||||||
var key ssh.Signer
|
var key ssh.Signer
|
||||||
if len(passphrase) > 0 {
|
if len(passphrase) > 0 {
|
||||||
key, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(passphrase))
|
key, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(passphrase))
|
||||||
@ -313,9 +311,9 @@ func CreateSshClient(assetId string) (*ssh.Client, error) {
|
|||||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", asset.IP, asset.Port)
|
addr := fmt.Sprintf("%s:%d", session.IP, session.Port)
|
||||||
|
|
||||||
sshClient, err := ssh.Dial("tcp", addr, config)
|
sshClient, err = ssh.Dial("tcp", addr, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -329,8 +327,8 @@ func WriteByteMessage(ws *websocket.Conn, p []byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSftpClient(assetId string) (sftpClient *sftp.Client, err error) {
|
func CreateSftpClient(session model.Session) (sftpClient *sftp.Client, err error) {
|
||||||
sshClient, err := CreateSshClient(assetId)
|
sshClient, err := CreateSshClientBySession(session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
27
pkg/model/user-attribute.go
Normal file
27
pkg/model/user-attribute.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "next-terminal/pkg/global"
|
||||||
|
|
||||||
|
const (
|
||||||
|
FontSize = "font-size"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserAttribute struct {
|
||||||
|
Id string `gorm:"index" json:"id"`
|
||||||
|
UserId string `gorm:"index" json:"userId"`
|
||||||
|
Name string `gorm:"index" json:"name"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserAttribute) TableName() string {
|
||||||
|
return "user_attributes"
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateUserAttribute(o *UserAttribute) error {
|
||||||
|
return global.DB.Create(o).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindUserAttributeByUserId(userId string) (o []UserAttribute, err error) {
|
||||||
|
err = global.DB.Where("user_id = ?", userId).Find(&o).Error
|
||||||
|
return o, err
|
||||||
|
}
|
@ -436,7 +436,7 @@ class Access extends Component {
|
|||||||
this.showMessage(result['message']);
|
this.showMessage(result['message']);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
document.title = result['data']['ip'] + ':' + result['data']['port'];
|
document.title = result['data']['name'];
|
||||||
return result['data']['id'];
|
return result['data']['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,10 @@ import {Terminal} from "xterm";
|
|||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import {wsServer} from "../../common/constants";
|
import {wsServer} from "../../common/constants";
|
||||||
import "./Console.css"
|
import "./Console.css"
|
||||||
import {getToken} from "../../utils/utils";
|
import {getToken, isEmpty} from "../../utils/utils";
|
||||||
import {FitAddon} from 'xterm-addon-fit';
|
import {FitAddon} from 'xterm-addon-fit';
|
||||||
import "./Access.css"
|
import "./Access.css"
|
||||||
|
import request from "../../common/request";
|
||||||
|
|
||||||
class AccessSSH extends Component {
|
class AccessSSH extends Component {
|
||||||
|
|
||||||
@ -18,15 +19,20 @@ class AccessSSH extends Component {
|
|||||||
fitAddon: undefined
|
fitAddon: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount = async () => {
|
||||||
|
|
||||||
let urlParams = new URLSearchParams(this.props.location.search);
|
let urlParams = new URLSearchParams(this.props.location.search);
|
||||||
let assetId = urlParams.get('assetId');
|
let assetId = urlParams.get('assetId');
|
||||||
|
|
||||||
|
let sessionId = await this.createSession(assetId);
|
||||||
|
if (isEmpty(sessionId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let params = {
|
let params = {
|
||||||
'width': this.state.width,
|
'width': this.state.width,
|
||||||
'height': this.state.height,
|
'height': this.state.height,
|
||||||
'assetId': assetId
|
'sessionId': sessionId
|
||||||
};
|
};
|
||||||
|
|
||||||
let paramStr = qs.stringify(params);
|
let paramStr = qs.stringify(params);
|
||||||
@ -104,6 +110,7 @@ class AccessSSH extends Component {
|
|||||||
let msg = JSON.parse(e.data);
|
let msg = JSON.parse(e.data);
|
||||||
switch (msg['type']) {
|
switch (msg['type']) {
|
||||||
case 'connected':
|
case 'connected':
|
||||||
|
term.clear();
|
||||||
console.log(msg['content'])
|
console.log(msg['content'])
|
||||||
this.onWindowResize();
|
this.onWindowResize();
|
||||||
break;
|
break;
|
||||||
@ -135,6 +142,16 @@ class AccessSSH extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createSession(assetsId) {
|
||||||
|
let result = await request.post(`/sessions?assetId=${assetsId}`);
|
||||||
|
if (result['code'] !== 1) {
|
||||||
|
this.showMessage(result['message']);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
document.title = result['data']['name'];
|
||||||
|
return result['data']['id'];
|
||||||
|
}
|
||||||
|
|
||||||
terminalSize() {
|
terminalSize() {
|
||||||
return {
|
return {
|
||||||
cols: Math.floor(this.state.width / 7.5),
|
cols: Math.floor(this.state.width / 7.5),
|
||||||
@ -169,8 +186,9 @@ class AccessSSH extends Component {
|
|||||||
<div ref='terminal' id='terminal' style={{
|
<div ref='terminal' id='terminal' style={{
|
||||||
height: this.state.height,
|
height: this.state.height,
|
||||||
width: this.state.width,
|
width: this.state.width,
|
||||||
|
backgroundColor: 'black',
|
||||||
overflowX: 'hidden',
|
overflowX: 'hidden',
|
||||||
overflowY: 'hidden'
|
overflowY: 'hidden',
|
||||||
}}/>
|
}}/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user