Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "github.com/jlaffaye/ftp"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- )
- var (
- Server = ""
- Login = ""
- Password = ""
- BasePath = "~"
- client *ftp.ServerConn
- file *os.File
- err error
- cur1, _ = ioutil.ReadFile("web/Home.html")
- homeScreenForm = string(cur1)
- cur2, _ = ioutil.ReadFile("web/Menu.html")
- menuScreenFormStart = string(cur2)
- cur3, _ = ioutil.ReadFile("web/Options.html")
- options = string(cur3)
- cur4, _ = ioutil.ReadFile("web/End.html")
- menuScreenFormEnd = string(cur4)
- //185.20.227.83
- )
- func HomeScreenHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, homeScreenForm)
- }
- func MenuScreenHandler(w http.ResponseWriter, r *http.Request) {
- if r.URL.Query().Get("server") != "" {
- Server = r.URL.Query().Get("server")
- }
- if r.URL.Query().Get("login") != "" {
- Login = r.URL.Query().Get("login")
- }
- if r.URL.Query().Get("pass") != "" {
- Password = r.URL.Query().Get("pass")
- }
- res := menuScreenFormStart
- res += "<center><p><h2>" + Server + "</h2></p>" + "<p><h3> Connected </h3></p>"
- res += options
- option := r.URL.Query().Get("options")
- switch option {
- case "delete":
- err = removeDirectory(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- case "make":
- err = makeDirectory(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- case "list":
- entries, err := listOfDirectories(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- res += "<p><b>" + r.URL.Query().Get("f1") + ": </b></p>"
- for _, e := range entries {
- res += "<p>" + e.Name + "</p>"
- }
- case "upload":
- err = uploadFile(r.URL.Query().Get("f1"), r.URL.Query().Get("f2"))
- if err != nil {
- log.Fatal(err)
- }
- case "read":
- str, err := readFile(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- res += "<p>" + str + "</p>"
- }
- res += menuScreenFormEnd
- fmt.Fprint(w, res)
- }
- func uploadFile(from string, to string) error {
- client, err = ftp.Dial(Server)
- if err != nil {
- return err
- }
- file, err = os.Open(BasePath + from)
- if err != nil {
- return err
- }
- reader := bufio.NewReader(file)
- err = client.Stor(to, reader)
- if err != nil {
- return err
- }
- return nil
- }
- func removeDirectory(path string) error {
- client, err = ftp.Dial(Server)
- if err != nil {
- return err
- }
- err = client.Delete(path)
- if err != nil {
- return err
- }
- return nil
- }
- func makeDirectory(dir string) error {
- client, err = ftp.Dial(Server)
- if err != nil {
- return err
- }
- err = client.MakeDir(dir)
- if err != nil {
- return err
- }
- return nil
- }
- func listOfDirectories(path string) ([]*ftp.Entry, error) {
- client, err = ftp.Dial(Server)
- if err != nil {
- return nil, err
- }
- entries, err := client.List(path)
- if err != nil {
- return nil, err
- }
- return entries, nil
- }
- func readFile(path string) (string, error) {
- client, err = ftp.Dial(Server)
- if err != nil {
- return "", err
- }
- file, err := client.Retr(path)
- if err != nil {
- return "", err
- }
- buffer, err := ioutil.ReadAll(file)
- if err != nil {
- return "", err
- }
- return string(buffer), nil
- }
- func main() {
- http.HandleFunc("/", HomeScreenHandler)
- http.HandleFunc("/menu", MenuScreenHandler)
- err = http.ListenAndServe(":9107", nil)
- if err != nil {
- log.Fatal("ListenAndServe: ", err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement