|
@@ -1,16 +1,19 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "bytes"
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
|
|
+ "log"
|
|
"net/http"
|
|
"net/http"
|
|
"net/url"
|
|
"net/url"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/labstack/echo"
|
|
"github.com/labstack/echo"
|
|
|
|
+ "golang.org/x/crypto/ssh"
|
|
)
|
|
)
|
|
|
|
|
|
type handler struct{}
|
|
type handler struct{}
|
|
@@ -209,9 +212,54 @@ func (h *handler) private(c echo.Context) error {
|
|
return c.String(http.StatusOK, "Welcome "+name+"!")
|
|
return c.String(http.StatusOK, "Welcome "+name+"!")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func Connect(user, pass, host string, cmd string) bytes.Buffer {
|
|
|
|
+ cipher := ssh.Config{
|
|
|
|
+ Ciphers: []string{"aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
|
|
|
|
+ }
|
|
|
|
+ config := &ssh.ClientConfig{
|
|
|
|
+ User: user,
|
|
|
|
+ Auth: []ssh.AuthMethod{
|
|
|
|
+ ssh.Password(pass),
|
|
|
|
+ },
|
|
|
|
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
+ Config: cipher,
|
|
|
|
+ }
|
|
|
|
+ conn, err := ssh.Dial("tcp", host, config)
|
|
|
|
+ // time.Sleep(1)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("Failed to dial: ", err)
|
|
|
|
+ }
|
|
|
|
+ sess, err := conn.NewSession()
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("Failed to create session: ", err)
|
|
|
|
+ }
|
|
|
|
+ stdin, err := sess.StdinPipe()
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("Failed to create session: ", err)
|
|
|
|
+ }
|
|
|
|
+ var bout bytes.Buffer
|
|
|
|
+ var berr bytes.Buffer
|
|
|
|
+ sess.Stdout = &bout
|
|
|
|
+ sess.Stderr = &berr
|
|
|
|
+ sess.Shell()
|
|
|
|
+ fmt.Fprintf(stdin, "%s\n", "terminal length 0")
|
|
|
|
+ fmt.Fprintf(stdin, "%s\n", cmd)
|
|
|
|
+ fmt.Fprintf(stdin, "\nexit\n")
|
|
|
|
+ fmt.Fprintf(stdin, "exit\n")
|
|
|
|
+ sess.Wait()
|
|
|
|
+ sess.Close()
|
|
|
|
+ // scanner := bufio.NewScanner(&bout)
|
|
|
|
+ // for scanner.Scan() {
|
|
|
|
+ // fmt.Println(scanner.Text())
|
|
|
|
+ // }
|
|
|
|
+ // fmt.Println(bout.String())
|
|
|
|
+ return bout
|
|
|
|
+}
|
|
|
|
+
|
|
func (h *handler) findMAC(c echo.Context) error {
|
|
func (h *handler) findMAC(c echo.Context) error {
|
|
user := c.Get("user").(*jwt.Token)
|
|
user := c.Get("user").(*jwt.Token)
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
name := claims["name"].(string)
|
|
name := claims["name"].(string)
|
|
|
|
+
|
|
return c.String(http.StatusOK, "Welcome "+name+"!")
|
|
return c.String(http.StatusOK, "Welcome "+name+"!")
|
|
}
|
|
}
|