Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add flag to force use of local control host address #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export class Client {
*
* @param timeout Timeout in milliseconds, use 0 for no timeout. Optional, default is 30 seconds.
*/
constructor(timeout = 30000) {
constructor(timeout = 30000, alwaysUseControlHost = false) {
this.ftp = new FTPContext(timeout)
this.prepareTransfer = this._enterFirstCompatibleMode([enterPassiveModeIPv6, enterPassiveModeIPv4])
this.prepareTransfer = this._enterFirstCompatibleMode([enterPassiveModeIPv6, enterPassiveModeIPv4(alwaysUseControlHost)])
this.parseList = parseListAutoDetect
this._progressTracker = new ProgressTracker()
}
Expand Down
33 changes: 18 additions & 15 deletions src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,25 @@ export function parseEpsvResponse(message: string): number {
/**
* Prepare a data socket using passive mode over IPv4.
*/
export async function enterPassiveModeIPv4(ftp: FTPContext): Promise<FTPResponse> {
const res = await ftp.request("PASV")
const target = parsePasvResponse(res.message)
if (!target) {
throw new Error("Can't parse PASV response: " + res.message)
}
// If the host in the PASV response has a local address while the control connection hasn't,
// we assume a NAT issue and use the IP of the control connection as the target for the data connection.
// We can't always perform this replacement because it's possible (although unlikely) that the FTP server
// indeed uses a different host for data connections.
const controlHost = ftp.socket.remoteAddress
if (ipIsPrivateV4Address(target.host) && controlHost && !ipIsPrivateV4Address(controlHost)) {
target.host = controlHost
export function enterPassiveModeIPv4(alwaysUseControlHost: boolean): (ftp: FTPContext) => Promise<FTPResponse> {
return async (ftp: FTPContext) => {
const res = await ftp.request("PASV")
const target = parsePasvResponse(res.message)
if (!target) {
throw new Error("Can't parse PASV response: " + res.message)
}
// If the host in the PASV response has a local address while the control connection hasn't,
// we assume a NAT issue and use the IP of the control connection as the target for the data connection.
// We can't always perform this replacement because it's possible (although unlikely) that the FTP server
// indeed uses a different host for data connections. Use 'alwaysUseControlHost' flag if you want to use the
// remote address even if both are local.
const controlHost = ftp.socket.remoteAddress
if (controlHost && ((ipIsPrivateV4Address(target.host) && !ipIsPrivateV4Address(controlHost)) || alwaysUseControlHost)) {
target.host = controlHost
}
await connectForPassiveTransfer(target.host, target.port, ftp)
return res
}
await connectForPassiveTransfer(target.host, target.port, ftp)
return res
}

/**
Expand Down