Skip to content

Commit

Permalink
v0.0.1 基础版本
Browse files Browse the repository at this point in the history
  • Loading branch information
何全 committed Jul 31, 2019
1 parent 971a68f commit 296d9b6
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 1 deletion.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# go-webssh
go版本 webssh

## 核心
> 本项目代码来自 https://github.com/dejavuzhou/felix ,只是把里面的webssh拿出来,修改了一下,做成webssh,特此说明.有需要可以查看此项目。

## 安装
> 修改 core/ssh.go 里面的账号密码地址等信息。 也可以自己修改成用密钥登录。
```go
func NewSshClient() (*ssh.Client, error) {
config := &ssh.ClientConfig{
Timeout: time.Second * 5,
User: "root",
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
}
//if h.Type == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password("123456")}
//} else {
// config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(h.Key)}
//}
addr := fmt.Sprintf("%s:%d", "192.168.100.200", 22)
c, err := ssh.Dial("tcp", addr, config)
if err != nil {
return nil, err
}
return c, nil
}

```
> go install
> go run main.go
## 前端
> 我测试的时候用得是 vue,你可以放进你们项目里面。在web/vue/index.vue里面,记得修改32行的 后端地址
> 也可以自己弄个普通 index.html ,放一个websocket连接即可。
> web/html 是普通版本index.html,未测试,仅供参考!

## demo

![SQL](static/demo/demo1.jpg)
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func Cors() gin.HandlerFunc {
func main() {
r := gin.Default()
r.Use(Cors())
r.GET("/ws/:id", internal.WsSsh)
r.GET("/ws/:id", core.WsSsh)
_ = r.Run() // listen and serve on 0.0.0.0:8080
}
7 changes: 7 additions & 0 deletions static/css/bootstrap.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions static/css/fullscreen.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions static/css/xterm.min.css

Large diffs are not rendered by default.

Binary file added static/demo/demo1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions static/js/bootstrap.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/js/fullscreen.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions static/js/jquery.min.js

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions static/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

jQuery(function($){

var status = $('#status');
// btn = $('.btn-primary');

$('form#connect').submit(function(event) {
event.preventDefault();

var form = $(this),
url = form.attr('action'),
type = form.attr('type');
// data = new FormData(this);

// var pk = data.get('privatekey');
// if (pk && pk.size > 16384) {
// status.text('Key size exceeds maximum value.');
// return;
// }

// status.text('');
// btn.prop('disabled', true);

$.ajax({
url: url,
type: type,
data: {hostname:'42.62.55.58',port:22,username:'root',password:'123456'},
success: callback
// cache: false,
// contentType: false,
// processData: false
});

});



function callback(msg) {
console.log(msg);
if (msg.status) {
status.text(msg.status);
// setTimeout(function(){
// btn.prop('disabled', false);
// }, 3000);
// return;
}

var ws_url = window.location.href.replace('http', 'ws'),
// join = (ws_url[ws_url.length-1] == '/' ? '' : '/'),

url = "ws://127.0.0.1:8005/" + 'ws?id=' + msg.id,
// url = ws_url + join + 'ws?id=' + msg.id,

socket = new WebSocket(url),
terminal = document.getElementById('#terminal'),
term = new Terminal({cursorBlink: true});
// alert(ws_url)
console.log(url);
term.on('data', function(data) {
// console.log(data);
socket.send(data);
});

socket.onopen = function(e) {
$('.container').hide();
term.open(terminal, true);
term.toggleFullscreen(true);
};

socket.onmessage = function(msg) {
// console.log(msg);
term.write(msg.data);
};

socket.onerror = function(e) {
console.log(e);
};

socket.onclose = function(e) {
console.log(e);
term.destroy();
$('.container').show();
// status.text(e.reason);
// btn.prop('disabled', false);
};
}
});
4 changes: 4 additions & 0 deletions static/js/popper.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/js/xterm.min.js

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions web/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> webssh 例子</title>


<link href="/static/css/xterm.min.css" rel="stylesheet" type="text/css"/>
<style>
body {
padding-bottom: 30px;
}

.terminal {
border: #000 solid 5px;
font-family: cursive;
{# font-family: Arial, Helvetica, Tahoma ,"Monaco", "DejaVu Sans Mono", "Liberation Mono", sans-serif;#}{# font-family: Tahoma, Helvetica, Arial, sans-serif;#}{# font-family: "\5B8B\4F53","","Monaco", "DejaVu Sans Mono", "Liberation Mono", "Microsoft YaHei", monospace;#} font-size: 15px;
{# color: #f0f0f0;#} background: #000;
{# width: 893px;#}{# height: 550px;#} box-shadow: rgba(0, 0, 0, 0.8) 2px 2px 20px;
}

.reverse-video {
color: #000;
background: #f0f0f0;
}

</style>
</head>
<body>

<div id="terms"></div>
</body>

<script src="/static/js/xterm.min.js"></script>
<script>
var socket = new WebSocket('ws://' + window.location.host + '/ws/1');

socket.onopen = function () {

var term = new Terminal();
term.open(document.getElementById('terms'));

term.on('data', function (data) {
console.log(data);
socket.send(data);
});

socket.onmessage = function (msg) {
console.log(msg);
console.log(msg.data);
term.write(msg.data);
};
socket.onerror = function (e) {
console.log(e);
};

socket.onclose = function (e) {
console.log(e);
term.destroy();
};
};

</script>

</html>
54 changes: 54 additions & 0 deletions web/vue/Console.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<template>
<div class="console"
id="terminal"></div>
</template>
<script>
import Terminal from './Xterm'
export default {
name: 'Console',
props: {
terminal: {
type: Object,
// eslint-disable-next-line vue/require-valid-default-prop
default: {}
},
url: ''
},
data () {
return {
term: null,
terminalSocket: null
}
},
methods: {
runRealTerminal () {
console.log('webSocket is finished')
},
errorRealTerminal () {
console.log('error')
},
closeRealTerminal () {
console.log('close')
}
},
mounted () {
console.log('pid : ' + this.terminal.pid + ' is on ready')
let terminalContainer = document.getElementById('terminal')
this.term = new Terminal()
this.term.open(terminalContainer)
// open websocket
console.log(this.url)
this.terminalSocket = new WebSocket(this.url)
this.terminalSocket.onopen = this.runRealTerminal
this.terminalSocket.onclose = this.closeRealTerminal
this.terminalSocket.onerror = this.errorRealTerminal
this.term.attach(this.terminalSocket)
this.term._initialized = true
console.log('mounted is going on')
},
beforeDestroy () {
this.terminalSocket.close()
this.term.destroy()
}
}
</script>
7 changes: 7 additions & 0 deletions web/vue/Xterm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Terminal } from 'xterm'
import * as fit from 'xterm/lib/addons/fit/fit'
import * as attach from 'xterm/lib/addons/attach/attach'
Terminal.applyAddon(fit)
Terminal.applyAddon(attach)

export default Terminal
44 changes: 44 additions & 0 deletions web/vue/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<template>
<div>
<Row>
<Card>

<br />
<div class="container">
<my-terminal :terminal="terminal"
:url="url"></my-terminal>
</div>

</Card>
</Row>

</div>
</template>

<script>
import Console from 'Console'
export default {
data () {
return {
title: 'webssh',
name: '',
namespace: '',
terminal: {
pid: 1,
name: 'terminal',
cols: 400,
rows: 400
},
url: `ws://192.168.42.1:8080/ws/${this.$route.params.name}?cols=188&rows=50`
}
},
methods: {
connection () {
console.log(this.$route.params.name)
}
},
components: {
'my-terminal': Console
}
}
</script>

0 comments on commit 296d9b6

Please sign in to comment.