v/vlib/net/ftp/ftp.v

264 lines
4.9 KiB
V
Raw Normal View History

module ftp
2019-12-27 19:08:44 +01:00
/*
2020-05-16 16:12:23 +02:00
basic ftp module
2019-12-27 19:08:44 +01:00
RFC-959
https://tools.ietf.org/html/rfc959
Methods:
ftp.connect(host)
ftp.login(user, passw)
2019-12-27 19:08:44 +01:00
pwd := ftp.pwd()
ftp.cd(folder)
dtp := ftp.pasv()
ftp.dir()
ftp.get(file)
dtp.read()
dtp.close()
ftp.close()
*/
import net
import io
2019-12-27 19:08:44 +01:00
const (
connected = 220
specify_password = 331
logged_in = 230
login_first = 503
anonymous = 530
open_data_connection = 150
close_data_connection = 226
command_ok = 200
denied = 550
passive_mode = 227
complete = 226
2019-12-27 19:08:44 +01:00
)
struct DTP {
mut:
conn net.TcpConn
reader io.BufferedReader
ip string
port int
2019-12-27 19:08:44 +01:00
}
fn (mut dtp DTP) read() ?[]byte {
2021-01-07 20:21:47 +01:00
mut data := []byte{}
mut buf := []byte{len: 1024}
for {
len := dtp.reader.read(mut buf) or { break }
if len == 0 {
break
}
data << buf[..len]
}
2019-12-27 19:08:44 +01:00
return data
}
fn (mut dtp DTP) close() {
dtp.conn.close()
2019-12-27 19:08:44 +01:00
}
struct FTP {
mut:
conn net.TcpConn
reader io.BufferedReader
2019-12-27 19:08:44 +01:00
buffer_size int
}
pub fn new() FTP {
mut f := FTP{}
f.buffer_size = 1024
return f
}
fn (mut ftp FTP) write(data string) ? {
2019-12-27 19:08:44 +01:00
$if debug {
println('FTP.v >>> $data')
}
ftp.conn.write('$data\r\n'.bytes()) ?
2019-12-27 19:08:44 +01:00
}
fn (mut ftp FTP) read() ?(int, string) {
mut data := ftp.reader.read_line() ?
2019-12-27 19:08:44 +01:00
$if debug {
println('FTP.v <<< $data')
}
if data.len < 5 {
return 0, ''
2019-12-27 19:08:44 +01:00
}
2020-01-19 13:53:13 +01:00
code := data[..3].int()
if data[3] == `-` {
2019-12-27 19:08:44 +01:00
for {
data = ftp.reader.read_line() ?
2020-01-19 13:53:13 +01:00
if data[..3].int() == code && data[3] != `-` {
2019-12-27 19:08:44 +01:00
break
}
}
}
return code, data
2019-12-27 19:08:44 +01:00
}
pub fn (mut ftp FTP) connect(ip string) ?bool {
ftp.conn = net.dial_tcp('$ip:21') ?
ftp.reader = io.new_buffered_reader(reader: io.make_reader(ftp.conn))
code, _ := ftp.read() ?
if code == connected {
2019-12-27 19:08:44 +01:00
return true
}
return false
}
pub fn (mut ftp FTP) login(user string, passwd string) ?bool {
ftp.write('USER $user') or {
$if debug {
println('ERROR sending user')
}
2019-12-27 19:08:44 +01:00
return false
}
mut code, _ := ftp.read() ?
if code == logged_in {
2019-12-27 19:08:44 +01:00
return true
}
if code != specify_password {
2019-12-27 19:08:44 +01:00
return false
}
ftp.write('PASS $passwd') or {
$if debug {
println('ERROR sending password')
}
2019-12-27 19:08:44 +01:00
return false
}
code, _ = ftp.read() ?
if code == logged_in {
2019-12-27 19:08:44 +01:00
return true
}
return false
}
pub fn (mut ftp FTP) close() ? {
ftp.write('QUIT') ?
ftp.conn.close()
2019-12-27 19:08:44 +01:00
}
pub fn (mut ftp FTP) pwd() ?string {
ftp.write('PWD') ?
_, data := ftp.read() ?
spl := data.split('"') // "
2019-12-27 19:08:44 +01:00
if spl.len >= 2 {
return spl[1]
}
return data
}
pub fn (mut ftp FTP) cd(dir string) ? {
ftp.write('CWD $dir') or { return }
mut code, mut data := ftp.read() ?
match int(code) {
denied {
$if debug {
println('CD $dir denied!')
}
2019-12-27 19:08:44 +01:00
}
complete {
code, data = ftp.read() ?
2019-12-27 19:08:44 +01:00
}
else {}
}
$if debug {
println('CD $data')
}
2019-12-27 19:08:44 +01:00
}
2021-01-07 20:21:47 +01:00
fn new_dtp(msg string) ?&DTP {
if !is_dtp_message_valid(msg) {
return error('Bad message')
2019-12-27 19:08:44 +01:00
}
ip, port := get_host_ip_from_dtp_message(msg)
2021-01-07 20:21:47 +01:00
mut dtp := &DTP{
2019-12-27 19:08:44 +01:00
ip: ip
port: port
}
2021-01-07 20:21:47 +01:00
conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
dtp.conn = conn
dtp.reader = io.new_buffered_reader(reader: io.make_reader(dtp.conn))
2019-12-27 19:08:44 +01:00
return dtp
}
2021-01-07 20:21:47 +01:00
fn (mut ftp FTP) pasv() ?&DTP {
ftp.write('PASV') ?
code, data := ftp.read() ?
$if debug {
println('pass: $data')
}
if code != passive_mode {
2019-12-27 19:08:44 +01:00
return error('pasive mode not allowed')
}
dtp := new_dtp(data) ?
2019-12-27 19:08:44 +01:00
return dtp
}
pub fn (mut ftp FTP) dir() ?[]string {
mut dtp := ftp.pasv() or { return error('Cannot establish data connection') }
ftp.write('LIST') ?
code, _ := ftp.read() ?
if code == denied {
return error('`LIST` denied')
2019-12-27 19:08:44 +01:00
}
if code != open_data_connection {
return error('Data channel empty')
2019-12-27 19:08:44 +01:00
}
list_dir := dtp.read() ?
result, _ := ftp.read() ?
if result != close_data_connection {
println('`LIST` not ok')
2019-12-27 19:08:44 +01:00
}
dtp.close()
2020-04-26 13:49:31 +02:00
mut dir := []string{}
sdir := list_dir.bytestr()
2019-12-27 19:08:44 +01:00
for lfile in sdir.split('\n') {
2020-05-16 16:12:23 +02:00
if lfile.len > 1 {
2019-12-27 19:08:44 +01:00
spl := lfile.split(' ')
2020-05-16 16:12:23 +02:00
dir << spl[spl.len - 1]
2019-12-27 19:08:44 +01:00
}
}
return dir
}
pub fn (mut ftp FTP) get(file string) ?[]byte {
mut dtp := ftp.pasv() or { return error('Cannot stablish data connection') }
ftp.write('RETR $file') ?
code, _ := ftp.read() ?
if code == denied {
return error('Permission denied')
2019-12-27 19:08:44 +01:00
}
if code != open_data_connection {
return error('Data connection not ready')
2019-12-27 19:08:44 +01:00
}
blob := dtp.read() ?
2019-12-27 19:08:44 +01:00
dtp.close()
return blob
}
fn is_dtp_message_valid(msg string) bool {
// An example of message:
// '227 Entering Passive Mode (209,132,183,61,48,218)'
return msg.contains('(') && msg.contains(')') && msg.contains(',')
}
fn get_host_ip_from_dtp_message(msg string) (string, int) {
mut par_start_idx := -1
mut par_end_idx := -1
for i, c in msg {
if c == `(` {
par_start_idx = i + 1
} else if c == `)` {
par_end_idx = i
}
}
data := msg[par_start_idx..par_end_idx].split(',')
ip := data[0..4].join('.')
port := data[4].int() * 256 + data[5].int()
return ip, port
}