Skip to content
Simon Schmidt edited this page Feb 22, 2016 · 1 revision
/*
 * Copyright(C) 2016 Simon Schmidt
 * 
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a copy of the MPL
 * was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 */
package main

import "net"

import "golang.org/x/crypto/ssh"

import "fmt"
import "github.com/maxymania/go-system/sshlib"
import "github.com/maxymania/go-system/sshlib/winssh"

import "os/exec"
import "io/ioutil"
import "io"
import "bufio"

var S *ssh.ServerConfig

var N = 0

var P *ssh.Permissions

var SC = make(chan *sshlib.ShellSession,100)

func copyout(i io.ReadCloser,c ssh.Channel){
	defer i.Close()
	io.Copy(c,i)
}

func copyin(o io.WriteCloser,c ssh.Channel){
	defer o.Close()
	cc := bufio.NewReader(c)
	buffer := make([]byte,1)
	for{
		b,e := cc.ReadByte()
		if e!=nil { return }
		if b>=0x20 {
			fmt.Printf("Char '%c' \\x%02x\n",int(b),int(b))
		}else{
			fmt.Printf("Char '#' \\x%02x\n",int(b))
		}
		buffer[0]=b
		o.Write(buffer)
	}
}

func handleSession(sl *sshlib.ShellSession) {
	winssh.HandleSess(sl,exec.Command("cmd.exe"))
	/*
	defer sl.Ch.Close()
	cmd := exec.Command("cmd.exe")
	xout,_ := cmd.StdoutPipe()
	xin,_ := cmd.StdinPipe()
	cmd.Start()
	go copyout(xout,sl.Ch)
	go copyin(xin,sl.Ch)
	cmd.Wait()
	*/
}

func handler(){
    for sc := range SC {
        go handleSession(sc)
    }
}

func handle(nc net.Conn) {
    sshlib.Handle(nc,SC,S)
}

func passwd_auth(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
    P := new(ssh.Permissions)
    P.CriticalOptions = make(map[string]string)
    P.Extensions = make(map[string]string)
    return P,nil
}

// it is disabled, for now.
func pubk_auth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
    return P,nil
}

func load_keys() bool {
    keys := []string{
        "k1.pem",
        "k2.pem",
        "k3.pem",
    }
    for _,k := range keys {
        b,e := ioutil.ReadFile(k)
        if e!=nil { return true }
        s,e := ssh.ParsePrivateKey(b)
        if e!=nil { return true }
        S.AddHostKey(s)
    }
    return false
}

func main() {
    S = new(ssh.ServerConfig)
    P = new(ssh.Permissions)
    P.CriticalOptions = make(map[string]string)
    P.Extensions = make(map[string]string)
    S.PasswordCallback = passwd_auth
    //S.PublicKeyCallback = pubk_auth

    if load_keys() { return }

    l,e := net.Listen("tcp",":64022")
    if e!=nil {
        fmt.Println(e)
        return
    }
    go handler()
    for {
        c,e := l.Accept()
        if e!=nil {
            fmt.Println(e)
            break
        }
        go handle(c)
    }
    fmt.Println(l.Close())
}
Clone this wiki locally