Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Compitable with no special public ip and ports range
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jul 10, 2016
1 parent e5ffd58 commit fcd8a9c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
9 changes: 6 additions & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (cmd commandEpsv) RequireAuth() bool {
}

func (cmd commandEpsv) Execute(conn *Conn, param string) {
addr := conn.PublicIp()
addr := conn.passiveListenIP()
lastIdx := strings.LastIndex(addr, ":")
if lastIdx <= 0 {
conn.writeMessage(425, "Data connection failed")
Expand Down Expand Up @@ -367,7 +367,9 @@ func (cmd commandList) Execute(conn *Conn, param string) {
conn.writeMessage(550, err.Error())
return
}

if !info.IsDir() {
conn.logger.Printf("%s is not a dir.\n", path)
return
}
var files []FileInfo
Expand Down Expand Up @@ -592,15 +594,16 @@ func (cmd commandPasv) RequireAuth() bool {
}

func (cmd commandPasv) Execute(conn *Conn, param string) {
socket, err := newPassiveSocket(conn.PublicIp(), conn.PassivePort(), conn.logger, conn.tlsConfig)
listenIP := conn.passiveListenIP()
socket, err := newPassiveSocket(listenIP, conn.PassivePort(), conn.logger, conn.tlsConfig)
if err != nil {
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
p1 := socket.Port() / 256
p2 := socket.Port() - (p1 * 256)
quads := strings.Split(conn.PublicIp(), ".")
quads := strings.Split(listenIP, ".")
target := fmt.Sprintf("(%s,%s,%s,%s,%d,%d)", quads[0], quads[1], quads[2], quads[3], p1, p2)
msg := "Entering Passive Mode " + target
conn.writeMessage(227, msg)
Expand Down
27 changes: 19 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,29 @@ func (conn *Conn) PublicIp() string {
return conn.server.PublicIp
}

func (conn *Conn) passiveListenIP() string {
if len(conn.PublicIp()) > 0 {
return conn.PublicIp()
}
return conn.conn.LocalAddr().String()
}

func (conn *Conn) PassivePort() int {
portRange := strings.Split(conn.server.PassivePorts, "-")
if len(conn.server.PassivePorts) > 0 {
portRange := strings.Split(conn.server.PassivePorts, "-")

if len(portRange) != 2 {
log.Println("empty port")
return 0
}
if len(portRange) != 2 {
log.Println("empty port")
return 0
}

minPort, _ := strconv.Atoi(strings.TrimSpace(portRange[0]))
maxPort, _ := strconv.Atoi(strings.TrimSpace(portRange[1]))
minPort, _ := strconv.Atoi(strings.TrimSpace(portRange[0]))
maxPort, _ := strconv.Atoi(strings.TrimSpace(portRange[1]))

return minPort + mrand.Intn(maxPort-minPort)
return minPort + mrand.Intn(maxPort-minPort)
}
// let system automatically chose one port
return 0
}

// returns a random 20 char string that can be used as a unique session ID
Expand Down

0 comments on commit fcd8a9c

Please sign in to comment.