示例#1
0
func initialize() {
	privkey_fname := util.AppBaseFileName() + ".privkey"
	privkey_bytes, err := ioutil.ReadFile(privkey_fname)
	if err != nil {
		log.Panicf("privkey load error: %s", err)
	}
	signer, err := ssh.ParsePrivateKey(privkey_bytes)
	if err != nil {
		log.Panicf("privkey parse error: %s", err)
	}
	clientConfig = &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
	}
	hostlist_fname := util.AppBaseFileName() + ".hostlist"
	f, err := os.Open(hostlist_fname)
	if err != nil {
		log.Panicf("hostlist open error: %s", err)
	}

	defer f.Close()
	scanner := bufio.NewScanner(f)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		hostlist = append(hostlist, scanner.Text())
	}
}
示例#2
0
func AppConfig() (*Config, error) {
	cfg_fname := util.AppBaseFileName() + ".config.json"
	log.Println("config    : ", cfg_fname)
	cf, err := load(cfg_fname)
	if err != nil {
		log.Fatalln("config load error: ", err)
		return nil, err // unreachable
	}
	return cf, nil
}
示例#3
0
func serve() {
	config := &ssh.ServerConfig{
		PasswordCallback:  passwordCallback,
		PublicKeyCallback: pubkeyCallback,
	}
	priv_fname := util.AppBaseFileName() + ".privkey"
	log.Print("loading private key from " + priv_fname + " ...")
	priv_bytes, err := ioutil.ReadFile(priv_fname)
	if err != nil {
		log.Panicln("private key read error:", err)
	}
	log.Print("parsing private key...")
	private, err := ssh.ParsePrivateKey(priv_bytes)
	if err != nil {
		log.Panicln("private key parse error:", err)
	}
	log.Print("key fingerprint:", fingerprintKey(private.PublicKey()))
	log.Print("adding private key to host...")
	config.AddHostKey(private)

	log.Println("creating listener for", LISTEN_HOST_AND_PORT, " ...")
	listener, err := net.Listen("tcp", LISTEN_HOST_AND_PORT)
	log.Println("entering in the main service loop ...")
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Println("Failed to accept incoming connection:", err)
			continue
		}
		log.Println("new connection accepted from", conn.RemoteAddr())
		log.Println("upgrading connection to ssh...")
		sshConn, chans, reqs, err := ssh.NewServerConn(conn, config)
		if err != nil {
			log.Println("handshake failed:", err)
			continue
		}
		log.Printf("New SSH connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
		go ssh.DiscardRequests(reqs)
		go handleChannels(chans)
	}

}