-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
何全
committed
Jul 31, 2019
1 parent
971a68f
commit 296d9b6
Showing
17 changed files
with
327 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 | ||
|
||
 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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,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); | ||
}; | ||
} | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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,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> |
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,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> |
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,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 |
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,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> |