parent
5683af821e
commit
f751271e4e
|
@ -17,7 +17,6 @@ basic ftp module
|
||||||
dtp.close()
|
dtp.close()
|
||||||
ftp.close()
|
ftp.close()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import net
|
import net
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
@ -94,13 +93,12 @@ fn (mut ftp FTP) read() ?(int, string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ftp FTP) connect(ip string) ?bool {
|
pub fn (mut ftp FTP) connect(ip string) ?bool {
|
||||||
conn := net.dial_tcp('$ip:21')?
|
ftp.conn = net.dial_tcp('$ip:21') ?
|
||||||
ftp.conn = conn
|
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
|
||||||
code, _ := ftp.read() ?
|
code, _ := ftp.read() ?
|
||||||
if code == connected {
|
if code == connected {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,9 +145,7 @@ pub fn ( mut ftp FTP) pwd() ?string {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ftp FTP) cd(dir string) ? {
|
pub fn (mut ftp FTP) cd(dir string) ? {
|
||||||
ftp.write('CWD $dir') or {
|
ftp.write('CWD $dir') or { return }
|
||||||
return
|
|
||||||
}
|
|
||||||
mut code, mut data := ftp.read() ?
|
mut code, mut data := ftp.read() ?
|
||||||
match int(code) {
|
match int(code) {
|
||||||
denied {
|
denied {
|
||||||
|
@ -172,11 +168,10 @@ fn new_dtp(msg string) ?DTP {
|
||||||
return error('Bad message')
|
return error('Bad message')
|
||||||
}
|
}
|
||||||
ip, port := get_host_ip_from_dtp_message(msg)
|
ip, port := get_host_ip_from_dtp_message(msg)
|
||||||
conn := net.dial_tcp('$ip:$port') or {
|
conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
|
||||||
return error('Cannot connect to the data channel')
|
|
||||||
}
|
|
||||||
dtp := DTP{
|
dtp := DTP{
|
||||||
conn: conn
|
conn: conn
|
||||||
|
reader: io.new_buffered_reader(reader: io.make_reader(conn))
|
||||||
ip: ip
|
ip: ip
|
||||||
port: port
|
port: port
|
||||||
}
|
}
|
||||||
|
@ -184,8 +179,7 @@ fn new_dtp(msg string) ?DTP {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut ftp FTP) pasv() ?DTP {
|
fn (mut ftp FTP) pasv() ?DTP {
|
||||||
ftp.write('PASV') or {
|
ftp.write('PASV') ?
|
||||||
}
|
|
||||||
code, data := ftp.read() ?
|
code, data := ftp.read() ?
|
||||||
$if debug {
|
$if debug {
|
||||||
println('pass: $data')
|
println('pass: $data')
|
||||||
|
@ -198,22 +192,19 @@ fn ( mut ftp FTP) pasv() ?DTP {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ftp FTP) dir() ?[]string {
|
pub fn (mut ftp FTP) dir() ?[]string {
|
||||||
mut dtp := ftp.pasv() or {
|
mut dtp := ftp.pasv() or { return error('Cannot establish data connection') }
|
||||||
return error('cannot establish data connection')
|
ftp.write('LIST') ?
|
||||||
}
|
|
||||||
ftp.write('LIST') or {
|
|
||||||
}
|
|
||||||
code, _ := ftp.read() ?
|
code, _ := ftp.read() ?
|
||||||
if code == denied {
|
if code == denied {
|
||||||
return error('LIST denied')
|
return error('`LIST` denied')
|
||||||
}
|
}
|
||||||
if code != open_data_connection {
|
if code != open_data_connection {
|
||||||
return error('data channel empty')
|
return error('Data channel empty')
|
||||||
}
|
}
|
||||||
list_dir := dtp.read() ?
|
list_dir := dtp.read() ?
|
||||||
result, _ := ftp.read() ?
|
result, _ := ftp.read() ?
|
||||||
if result != close_data_connection {
|
if result != close_data_connection {
|
||||||
println('LIST not ok')
|
println('`LIST` not ok')
|
||||||
}
|
}
|
||||||
dtp.close()
|
dtp.close()
|
||||||
mut dir := []string{}
|
mut dir := []string{}
|
||||||
|
@ -228,11 +219,8 @@ pub fn ( mut ftp FTP) dir() ?[]string {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ftp FTP) get(file string) ?[]byte {
|
pub fn (mut ftp FTP) get(file string) ?[]byte {
|
||||||
mut dtp := ftp.pasv() or {
|
mut dtp := ftp.pasv() or { return error('Cannot stablish data connection') }
|
||||||
return error('Cannot stablish data connection')
|
ftp.write('RETR $file') ?
|
||||||
}
|
|
||||||
ftp.write('RETR $file') or {
|
|
||||||
}
|
|
||||||
code, _ := ftp.read() ?
|
code, _ := ftp.read() ?
|
||||||
if code == denied {
|
if code == denied {
|
||||||
return error('Permission denied')
|
return error('Permission denied')
|
||||||
|
|
Loading…
Reference in New Issue