You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
6.2 KiB
Go

8 years ago
package obfssh
import (
"fmt"
"net"
"time"
"github.com/fangdingjun/go-log"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
8 years ago
)
// Server is server connection
type Server struct {
conn net.Conn
sshConn *ssh.ServerConn
forwardedPorts map[string]net.Listener
exitCh chan struct{}
8 years ago
}
// NewServer create a new struct for Server
//
// c is net.Conn
//
// config is &ssh.ServerConfig
//
// conf is the server configure
//
8 years ago
//
func NewServer(c net.Conn, config *ssh.ServerConfig, conf *Conf) (*Server, error) {
sshConn, ch, req, err := ssh.NewServerConn(&TimedOutConn{c, 15 * 60 * time.Second}, config)
8 years ago
if err != nil {
return nil, err
}
sc := &Server{
conn: c,
8 years ago
sshConn: sshConn,
forwardedPorts: map[string]net.Listener{},
exitCh: make(chan struct{})}
8 years ago
go sc.handleGlobalRequest(req)
go sc.handleNewChannelRequest(ch)
return sc, nil
}
// Run waits for server connection finish
func (sc *Server) Run() {
sc.sshConn.Wait()
log.Debugf("ssh connection closed")
8 years ago
sc.close()
}
func (sc *Server) close() {
log.Debugf("close connection")
8 years ago
sc.sshConn.Close()
//log.Debugf( "close listener")
8 years ago
for _, l := range sc.forwardedPorts {
log.Debugf("close listener %s", l.Addr())
8 years ago
l.Close()
}
}
func (sc *Server) handleNewChannelRequest(ch <-chan ssh.NewChannel) {
for newch := range ch {
log.Debugf("request channel %s", newch.ChannelType())
8 years ago
switch newch.ChannelType() {
case "session":
go sc.handleSession(newch)
continue
8 years ago
case "direct-tcpip":
go handleDirectTcpip(newch)
continue
}
log.Debugf("reject channel request %s", newch.ChannelType())
8 years ago
newch.Reject(ssh.UnknownChannelType, "unknown channel type")
}
}
func (sc *Server) handleGlobalRequest(req <-chan *ssh.Request) {
for r := range req {
log.Debugf("global request %s", r.Type)
8 years ago
switch r.Type {
case "tcpip-forward":
log.Debugf("request port forward")
8 years ago
go sc.handleTcpipForward(r)
continue
case "cancel-tcpip-forward":
log.Debugf("request cancel port forward")
8 years ago
go sc.handleCancelTcpipForward(r)
continue
}
8 years ago
if r.WantReply {
r.Reply(false, nil)
}
}
}
func serveSFTP(ch ssh.Channel) {
defer ch.Close()
server, err := sftp.NewServer(ch)
if err != nil {
log.Debugf("start sftp server failed: %s", err)
return
}
if err := server.Serve(); err != nil {
log.Debugf("sftp server finished with error: %s", err)
return
8 years ago
}
}
type directTcpipMsg struct {
Raddr string
Rport uint32
Laddr string
Lport uint32
}
type args struct {
Arg string
}
8 years ago
func (sc *Server) handleSession(newch ssh.NewChannel) {
ch, req, err := newch.Accept()
if err != nil {
log.Errorf("%s", err.Error())
8 years ago
return
}
var cmd args
ret := false
for r := range req {
switch r.Type {
case "subsystem":
if err := ssh.Unmarshal(r.Payload, &cmd); err != nil {
ret = false
} else {
if cmd.Arg != "sftp" { // only support sftp
ret = false
} else {
ret = true
log.Debugf("handle sftp request")
go serveSFTP(ch)
}
}
default:
ret = false
}
log.Debugf("session request %s, reply %v", r.Type, ret)
if r.WantReply {
r.Reply(ret, nil)
8 years ago
}
}
}
func handleDirectTcpip(newch ssh.NewChannel) {
var r directTcpipMsg
data := newch.ExtraData()
8 years ago
err := ssh.Unmarshal(data, &r)
if err != nil {
log.Debugf("invalid ssh parameter")
8 years ago
newch.Reject(ssh.ConnectionFailed, "invalid argument")
return
}
log.Debugf("create connection to %s:%d", r.Raddr, r.Rport)
rconn, err := dialer.Dial("tcp", net.JoinHostPort(r.Raddr, fmt.Sprintf("%d", r.Rport)))
8 years ago
if err != nil {
log.Errorf("%s", err.Error())
8 years ago
newch.Reject(ssh.ConnectionFailed, "invalid argument")
return
}
8 years ago
channel, requests, err := newch.Accept()
if err != nil {
rconn.Close()
log.Errorf("%s", err.Error())
8 years ago
return
}
8 years ago
//log.Println("forward")
go ssh.DiscardRequests(requests)
8 years ago
PipeAndClose(channel, rconn)
}
type tcpipForwardAddr struct {
Addr string
Port uint32
}
func (sc *Server) handleCancelTcpipForward(req *ssh.Request) {
var a tcpipForwardAddr
if err := ssh.Unmarshal(req.Payload, &a); err != nil {
log.Errorf("invalid ssh parameter for cancel port forward")
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
k := fmt.Sprintf("%s:%d", a.Addr, a.Port)
if l, ok := sc.forwardedPorts[k]; ok {
l.Close()
delete(sc.forwardedPorts, k)
}
if req.WantReply {
req.Reply(true, nil)
}
}
func (sc *Server) handleTcpipForward(req *ssh.Request) {
var addr tcpipForwardAddr
if err := ssh.Unmarshal(req.Payload, &addr); err != nil {
log.Errorf("parse ssh data error: %s", err)
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
if addr.Port > 65535 || addr.Port < 0 {
log.Errorf("invalid port %d", addr.Port)
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
ip := net.ParseIP(addr.Addr)
if ip == nil {
log.Errorf("invalid ip %d", addr.Port)
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
k := fmt.Sprintf("%s:%d", addr.Addr, addr.Port)
if _, ok := sc.forwardedPorts[k]; ok {
// port in use
log.Errorf("port in use: %s", k)
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
//log.Debugf( "get request for addr: %s, port: %d", addr.Addr, addr.Port)
8 years ago
l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: ip, Port: int(addr.Port)})
if err != nil {
// listen failed
log.Errorf("%s", err.Error())
8 years ago
if req.WantReply {
req.Reply(false, nil)
}
return
}
a1 := l.Addr()
log.Debugf("Listening port %s", a1)
8 years ago
p := struct {
Port uint32
}{
uint32(a1.(*net.TCPAddr).Port),
}
sc.forwardedPorts[k] = l
if req.WantReply {
req.Reply(true, ssh.Marshal(p))
}
for {
c, err := l.Accept()
if err != nil {
log.Errorf("%s", err.Error())
8 years ago
return
}
log.Debugf("accept connection from %s", c.RemoteAddr())
8 years ago
go func(c net.Conn) {
laddr := c.LocalAddr()
raddr := c.RemoteAddr()
a2 := struct {
laddr string
lport uint32
raddr string
rport uint32
}{
addr.Addr,
uint32(laddr.(*net.TCPAddr).Port),
raddr.(*net.TCPAddr).IP.String(),
uint32(raddr.(*net.TCPAddr).Port),
}
ch, r, err := sc.sshConn.OpenChannel("forwarded-tcpip", ssh.Marshal(a2))
if err != nil {
log.Errorf("forward port failed: %s", err.Error())
8 years ago
c.Close()
return
}
go ssh.DiscardRequests(r)
PipeAndClose(c, ch)
}(c)
}
}