Advertisement
Void-voiD

Untitled

Oct 15th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "fmt"
  6. "github.com/jlaffaye/ftp"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "os"
  11. )
  12.  
  13. var (
  14. Server = "185.20.227.83:9212"
  15. Login = "admin"
  16. Password = "123456"
  17. BasePath = "/home/iu9_33_03/GoProjects/src/Laba1"
  18. client *ftp.ServerConn
  19. file *os.File
  20. err error
  21. cur1, _ = ioutil.ReadFile("web/Home.html")
  22. homeScreenForm = string(cur1)
  23. cur2, _ = ioutil.ReadFile("web/Menu.html")
  24. menuScreenFormStart = string(cur2)
  25. cur3, _ = ioutil.ReadFile("web/Options.html")
  26. options = string(cur3)
  27. cur4, _ = ioutil.ReadFile("web/End.html")
  28. menuScreenFormEnd = string(cur4)
  29. )
  30.  
  31. func HomeScreenHandler(w http.ResponseWriter, r *http.Request) {
  32. fmt.Fprintf(w, homeScreenForm)
  33. }
  34.  
  35. func MenuScreenHandler(w http.ResponseWriter, r *http.Request) {
  36. /*
  37. if r.URL.Query().Get("server") != "" {
  38. Server = r.URL.Query().Get("server")
  39. }
  40. if r.URL.Query().Get("login") != "" {
  41. Login = r.URL.Query().Get("login")
  42. }
  43. if r.URL.Query().Get("pass") != "" {
  44. Password = r.URL.Query().Get("pass")
  45. }
  46. */
  47.  
  48. res := menuScreenFormStart
  49. res += "<center><p><h2>" + Server + "</h2></p>" + "<p><h3> Connected </h3></p>"
  50. res += options
  51. option := r.URL.Query().Get("options")
  52.  
  53. switch option {
  54. case "delete":
  55. err = removeDirectory(r.URL.Query().Get("f1"))
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. case "make":
  60. err = makeDirectory(r.URL.Query().Get("f1"))
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. case "list":
  65. entries, err := listOfDirectories(r.URL.Query().Get("f1"))
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. res += "<p><b>" + r.URL.Query().Get("f1") + ": </b></p>"
  70. for _, e := range entries {
  71. res += "<p>" + e.Name + "</p>"
  72. }
  73. case "upload":
  74. err = uploadFile(r.URL.Query().Get("f1"), r.URL.Query().Get("f2"))
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. case "read":
  79. str, err := readFile(r.URL.Query().Get("f1"))
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. res += "<p>" + str + "</p>"
  84. case "currentDir":
  85. str, err := currentDirectory()
  86. if err != nil {
  87. log.Fatal(err)
  88. }
  89. res += "<p>" + str + "</p>"
  90. case "change":
  91. err := changeDirectory(r.URL.Query().Get("f1"))
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. }
  96. res += menuScreenFormEnd
  97. fmt.Fprint(w, res)
  98. }
  99.  
  100. func uploadFile(from string, to string) error {
  101. file, err = os.Open(BasePath + from)
  102. if err != nil {
  103. return err
  104. }
  105. reader := bufio.NewReader(file)
  106. err = client.Stor(to, reader)
  107. if err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112.  
  113. func removeDirectory(path string) error {
  114. err = client.Delete(path)
  115. if err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120.  
  121. func makeDirectory(dir string) error {
  122. err = client.MakeDir(dir)
  123. if err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128.  
  129. func listOfDirectories(path string) ([]*ftp.Entry, error) {
  130. entries, err := client.List(path)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return entries, nil
  135. }
  136.  
  137. func readFile(path string) (string, error) {
  138. file, err := client.Retr(path)
  139. if err != nil {
  140. return "", err
  141. }
  142. buffer, err := ioutil.ReadAll(file)
  143. if err != nil {
  144. return "", err
  145. }
  146. return string(buffer), nil
  147. }
  148.  
  149. func currentDirectory() (string, error) {
  150. name, err := client.CurrentDir()
  151. if err != nil {
  152. return "", err
  153. }
  154. return name, nil
  155. }
  156.  
  157. func changeDirectory(path string) error {
  158. err = client.ChangeDir(path)
  159. if err != nil {
  160. return err
  161. }
  162. return nil
  163. }
  164.  
  165. func main() {
  166. client, err = ftp.Connect(Server)
  167. if err != nil {
  168. log.Fatal(err)
  169. }
  170. err = client.Login(Login, Password)
  171. if err != nil {
  172. log.Fatal(err)
  173. }
  174. http.HandleFunc("/", HomeScreenHandler)
  175. http.HandleFunc("/menu", MenuScreenHandler)
  176. err = http.ListenAndServe(":9212", nil)
  177. if err != nil {
  178. log.Fatal("ListenAndServe: ", err)
  179. }
  180. err = client.Quit()
  181. if err != nil {
  182. log.Fatal(err)
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement