From 8fc35f3a355a92b23b584f4df8045b719bf46ff3 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Mon, 14 Jun 2021 08:56:43 +0300 Subject: [PATCH] Removed unnecessary -sending flag --- README.md | 44 +++++++++++++++++++++--------------------- main.go | 57 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 6a08095..9639320 100644 --- a/README.md +++ b/README.md @@ -27,26 +27,6 @@ Thus, with a connection and a way of communication, the sender will send some pa --- -## Usage -`./FTU [FLAGS_HERE]` or `FTU [FLAGS_HERE]` - -### Flags - -- `-sending` (bool) - if true - creates a server (sender) (also need to provide a `-sharefile` flag in that case), if false - creates a client (receiver) -- `-port` (int) - specifies a port; if `-sending` == true - listens on that port, else - connects to given port -- `addr` (string) - specifies an address to connect to (used when `-sending=false`) -- `-sharefile` (string) - specifies path to a file you want to share (used in pair with `-sending=true`), if given a valid path - a server will offer to share this file to a client -- `-downloadto` (string) - specifies path to a folder where the client wants to store downloaded file - -### Examples - -- `./FTU -sending=true -sharefile="/home/some_path_here/FILETOSHARE.zip"` - creates a server that will share `FILETOSHARE.zip` on port `8080` -- `./FTU -sending=true -sharefile="/home/some_path_here/FILETOSHARE.zip" - port=727` - same as before, but on port `727` -- `./FTU -sending=false -downloadto="/home/some_path_here/Downloads/" -addr="192.168.1.104"` - creates a client (receiver) that will try to connect to `192.168.1.104` (local device) on port `8080` and if successful - downloads a file to given path -- `./FTU -sending=false -downloadto="/home/some_path_here/Downloads/" -addr=145.125.53.212 -port=8888` - same as before, but will try to connect to `145.125.53.212` on port `8888` - - ---- ## Known issues|problems|lack of features|reasons why it`s bad - **VERY** slow; somewhat FIXED - [x], now **faster** than before @@ -56,19 +36,39 @@ Thus, with a connection and a way of communication, the sender will send some pa - Lack of information about the process of transferring (ETA, lost packets, etc.); FIXED - [ ] - No way to verify if the transferred file is not corrupted; FIXED via checksum- [x] - No encryption; FIXED - [ ] -- Messy and hard to follow code && file structure; partially FIXED (protocol is looking fairly good rn) - [ X ] +- Messy and hard to follow code && file structure; FIXED? - [x] - No way to stop the download/upload and resume it later or even during the next connection; FIXED - [ ] - No tests; FIXED - [ ] ## Good points - It works. +--- + +## Usage +`./FTU [FLAGS_HERE]` or `FTU [FLAGS_HERE]` + +### Flags + +- `-port` (int) - specifies a working port (if sending - listens on this port, else - tries to connect to this port); +- `addr` (string) - specifies an address to connect to; +- `-sharefile` (string) - specifies path to a file you want to share, if given a valid path - sender will offer to download this file to receiver; +- `-downloadto` (string) - specifies path to a folder where the receiver wants to store downloaded file; + +### Examples + +- `./FTU -sharefile="/home/some_path_here/FILETOSHARE.zip"` - creates a server that will share `FILETOSHARE.zip` on port `8080` +- `./FTU -sharefile="/home/some_path_here/FILETOSHARE.zip" - port=727` - same as before, but on port `727` +- `./FTU -downloadto="/home/some_path_here/Downloads/" -addr="192.168.1.104"` - creates a client (receiver) that will try to connect to `192.168.1.104` (local device) on port `8080` and if successful - downloads a file to given path +- `./FTU -downloadto="/home/some_path_here/Downloads/" -addr=145.125.53.212 -port=8888` - same as before, but will try to connect to `145.125.53.212` on port `8888` + + --- ## IMPORTANT NOTE This is NOT intended to be a serious application. I'm learning and this is a product of my curiosity. If you're a beginner too, please don't try to find something useful in my code, I am not an expert. -Also, this utility only works if both the server and the client have a port-forwarding enabled and configured. Fortunatelly, locally it works without any port-forwarding. +Also, this utility only works if both the server and the client have a port-forwarding|virtual server enabled and configured. Fortunatelly, locally it works without any port-forwarding|virtual servers. --- diff --git a/main.go b/main.go index 5265f7b..b42aa61 100644 --- a/main.go +++ b/main.go @@ -11,59 +11,66 @@ import ( ) // flags -var PORT *int = flag.Int("port", 8080, "Specifies a port for a sender") +var PORT *int = flag.Int("port", 8080, "Specifies a port for a sender|port to connect to") var SENDERADDR *string = flag.String("addr", "", "Specifies an IP for connection") -var SENDING *bool = flag.Bool("sending", false, "Send or receive") var DOWNLOADSFOLDER *string = flag.String("downloadto", "", "Specifies where the receiver will store downloaded file") var SHAREDFILE *string = flag.String("sharefile", "", "Specifies what file sender will send") +var SENDING bool + // helpMessage var HELPMSG string = ` -"-port", default: 8080, Specifies a port for a sender +"-port", default: 8080, Specifies a port for a sender|port to connect to "-addr", default: "", Specifies an IP for connection -"-sending", default: false, Send or receive "-downloadto", default: "", Specifies where the receiver will store downloaded file "-sharefile", default: "", Specifies what file sender will send` // Input-validation -func checkFlags() { - if *SENDING { - if strings.TrimSpace(*SHAREDFILE) == "" { - fmt.Println("No file specified !\n", HELPMSG) - os.Exit(1) - } - if *PORT <= 0 { - fmt.Println("Invalid port !\n", HELPMSG) - os.Exit(1) - } - } else if !*SENDING { - if strings.TrimSpace(*SENDERADDR) == "" { - fmt.Println("Invalid IP address !\n", HELPMSG) - os.Exit(1) +func processFlags() { + if *PORT < 0 { + fmt.Println("Invalid port !\n", HELPMSG) + os.Exit(-1) + } + + // going to send file -> sending + if strings.TrimSpace(*SHAREDFILE) != "" { + SENDING = true + } + // specifying address to connect to -> receiving + if strings.TrimSpace(*SENDERADDR) != "" { + if SENDING { + fmt.Println("Cannot specify an address when sharing !\n", HELPMSG) + os.Exit(-1) } - if strings.TrimSpace(*DOWNLOADSFOLDER) == "" { - *DOWNLOADSFOLDER = "./downloads/" - fmt.Println("Empty downloads folder. Changed to ./downloads/") + SENDING = false + } + // specifying path to download to -> receiving + if strings.TrimSpace(*DOWNLOADSFOLDER) != "" { + if SENDING { + fmt.Println("Cannot specify a downloads directory when sharing !\n", HELPMSG) + os.Exit(-1) } + SENDING = false } + } // parse flags, validate given values func init() { flag.Parse() - checkFlags() + processFlags() } func main() { - if *SENDING { + if SENDING { // 1) create sender -> 2) wait for a connection ->| - // 3) send fileinfo packet -> 4) if accepted - upload file + // 3) send info about the file -> 4) if accepted - upload file sender := sender.NewSender(*PORT, *SHAREDFILE) sender.WaitForConnection() sender.MainLoop() } else { - // 1) create receiver -> 2) try to connect to a sender -> 3) wait for a fileinfo packet ->| + // 1) create receiver -> 2) try to connect to a sender -> 3) wait for an info on the file ->| // 4) accept or refuse -> 5) download|don`t_download file receiver := receiver.NewReceiver(*DOWNLOADSFOLDER) receiver.Connect(fmt.Sprintf("%s:%d", *SENDERADDR, *PORT))