network.ftp.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "imuslab.com/arozos/mod/common"
  9. prout "imuslab.com/arozos/mod/prouter"
  10. ftp "imuslab.com/arozos/mod/storage/ftp"
  11. )
  12. /*
  13. FTP Server related handlers
  14. */
  15. var (
  16. ftpServer *ftp.Handler
  17. )
  18. //Handle init of the FTP server endpoints
  19. func FTPServerInit() {
  20. //Register FTP Server Setting page
  21. registerSetting(settingModule{
  22. Name: "FTP Server",
  23. Desc: "File Transfer Protocol Server",
  24. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  25. Group: "Network",
  26. StartDir: "SystemAO/disk/ftp.html",
  27. RequireAdmin: true,
  28. })
  29. //Register FTP Endpoints
  30. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  31. ModuleName: "System Setting",
  32. AdminOnly: true,
  33. UserHandler: userHandler,
  34. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  35. errorHandlePermissionDenied(w, r)
  36. },
  37. })
  38. //Create database related tables
  39. sysdb.NewTable("ftp")
  40. defaultEnable := false
  41. if sysdb.KeyExists("ftp", "default") {
  42. sysdb.Read("ftp", "default", &defaultEnable)
  43. } else {
  44. sysdb.Write("ftp", "default", false)
  45. }
  46. //Enable this service
  47. if defaultEnable {
  48. storageFTPServerStart()
  49. }
  50. adminRouter.HandleFunc("/system/storage/ftp/start", storageHandleFTPServerStart)
  51. adminRouter.HandleFunc("/system/storage/ftp/stop", storageHandleFTPServerStop)
  52. adminRouter.HandleFunc("/system/storage/ftp/upnp", storageHandleFTPuPnP)
  53. adminRouter.HandleFunc("/system/storage/ftp/status", storageHandleFTPServerStatus)
  54. adminRouter.HandleFunc("/system/storage/ftp/updateGroups", storageHandleFTPAccessUpdate)
  55. adminRouter.HandleFunc("/system/storage/ftp/setPort", storageHandleFTPSetPort)
  56. adminRouter.HandleFunc("/system/storage/ftp/passivemode", storageHandleFTPPassiveModeSettings)
  57. }
  58. /*
  59. Handle the settings for passive mode related files
  60. Example set commands
  61. set=ip&ip=123.456.789.1
  62. set=mode&passive=true
  63. */
  64. func storageHandleFTPPassiveModeSettings(w http.ResponseWriter, r *http.Request) {
  65. set, err := common.Mv(r, "set", true)
  66. if err != nil {
  67. common.SendErrorResponse(w, "Invalid set type")
  68. return
  69. }
  70. if set == "ip" {
  71. //Updat the public up addr
  72. ip, err := common.Mv(r, "ip", true)
  73. if err != nil {
  74. common.SendErrorResponse(w, "Invalid ip given")
  75. return
  76. }
  77. sysdb.Write("ftp", "publicip", ip)
  78. } else if set == "mode" {
  79. //Update the passive mode setting
  80. passive, err := common.Mv(r, "passive", true)
  81. if err != nil {
  82. common.SendErrorResponse(w, "Invalid passive option (true/false)")
  83. return
  84. }
  85. log.Println("Updatng FTP Server PassiveMode to", passive)
  86. if passive == "true" {
  87. sysdb.Write("ftp", "passive", true)
  88. } else {
  89. sysdb.Write("ftp", "passive", false)
  90. }
  91. } else {
  92. common.SendErrorResponse(w, "Unknown setting filed")
  93. return
  94. }
  95. //Restart the FTP server if it is running now
  96. if ftpServer != nil && ftpServer.ServerRunning {
  97. storageFTPServerStart()
  98. }
  99. }
  100. //Start the FTP Server by request
  101. func storageHandleFTPServerStart(w http.ResponseWriter, r *http.Request) {
  102. err := storageFTPServerStart()
  103. if err != nil {
  104. common.SendErrorResponse(w, err.Error())
  105. }
  106. //Remember the FTP server status
  107. sysdb.Write("ftp", "default", true)
  108. common.SendOK(w)
  109. }
  110. //Stop the FTP server by request
  111. func storageHandleFTPServerStop(w http.ResponseWriter, r *http.Request) {
  112. if ftpServer != nil {
  113. ftpServer.Close()
  114. }
  115. sysdb.Write("ftp", "default", false)
  116. log.Println("FTP Server Stopped")
  117. common.SendOK(w)
  118. }
  119. //Update UPnP setting on FTP server
  120. func storageHandleFTPuPnP(w http.ResponseWriter, r *http.Request) {
  121. enable, _ := common.Mv(r, "enable", false)
  122. if enable == "true" {
  123. log.Println("Enabling UPnP on FTP Server Port")
  124. sysdb.Write("ftp", "upnp", true)
  125. } else {
  126. log.Println("Disabling UPnP on FTP Server Port")
  127. sysdb.Write("ftp", "upnp", false)
  128. }
  129. //Restart FTP Server if server is running
  130. if ftpServer != nil && ftpServer.ServerRunning {
  131. storageFTPServerStart()
  132. }
  133. common.SendOK(w)
  134. }
  135. //Update access permission on FTP server
  136. func storageHandleFTPAccessUpdate(w http.ResponseWriter, r *http.Request) {
  137. //Get groups paramter from post req
  138. groupString, err := common.Mv(r, "groups", true)
  139. if err != nil {
  140. common.SendErrorResponse(w, "groups not defined")
  141. return
  142. }
  143. //Prase it
  144. groups := []string{}
  145. err = json.Unmarshal([]byte(groupString), &groups)
  146. if err != nil {
  147. common.SendErrorResponse(w, "Unable to parse groups")
  148. return
  149. }
  150. log.Println("Updating FTP Access group to: ", groups)
  151. //Set the accessable group
  152. ftp.UpdateAccessableGroups(sysdb, groups)
  153. common.SendOK(w)
  154. }
  155. func storageHandleFTPSetPort(w http.ResponseWriter, r *http.Request) {
  156. port, err := common.Mv(r, "port", true)
  157. if err != nil {
  158. common.SendErrorResponse(w, "Port not defined")
  159. return
  160. }
  161. //Try parse the port into int
  162. portInt, err := strconv.Atoi(port)
  163. if err != nil {
  164. common.SendErrorResponse(w, "Invalid port number")
  165. return
  166. }
  167. //Update the database port configuration
  168. sysdb.Write("ftp", "port", portInt)
  169. //Restart the FTP server
  170. storageFTPServerStart()
  171. common.SendOK(w)
  172. }
  173. func storageHandleFTPServerStatus(w http.ResponseWriter, r *http.Request) {
  174. type ServerStatus struct {
  175. Enabled bool
  176. Port int
  177. AllowUPNP bool
  178. UPNPEnabled bool
  179. FTPUpnpEnabled bool
  180. PublicAddr string
  181. PassiveMode bool
  182. UserGroups []string
  183. }
  184. enabled := false
  185. if ftpServer != nil && ftpServer.ServerRunning {
  186. enabled = true
  187. }
  188. serverPort := 21
  189. if sysdb.KeyExists("ftp", "port") {
  190. sysdb.Read("ftp", "port", &serverPort)
  191. }
  192. enableUPnP := false
  193. if sysdb.KeyExists("ftp", "upnp") {
  194. sysdb.Read("ftp", "upnp", &enableUPnP)
  195. }
  196. userGroups := []string{}
  197. if sysdb.KeyExists("ftp", "groups") {
  198. sysdb.Read("ftp", "groups", &userGroups)
  199. }
  200. ftpUpnp := false
  201. if ftpServer != nil && ftpServer.UPNPEnabled {
  202. ftpUpnp = true
  203. }
  204. publicAddr := ""
  205. if UPNP != nil && UPNP.ExternalIP != "" && ftpUpnp == true {
  206. publicAddr = UPNP.ExternalIP
  207. } else {
  208. manualPublicIpEntry := ""
  209. if sysdb.KeyExists("ftp", "publicip") {
  210. sysdb.Read("ftp", "publicip", &manualPublicIpEntry)
  211. }
  212. publicAddr = manualPublicIpEntry
  213. }
  214. forcePassiveMode := false
  215. if ftpUpnp == true {
  216. forcePassiveMode = true
  217. } else {
  218. if sysdb.KeyExists("ftp", "passive") {
  219. sysdb.Read("ftp", "passive", &forcePassiveMode)
  220. }
  221. if forcePassiveMode {
  222. //Read the ip setting from database
  223. manualPublicIpEntry := ""
  224. if sysdb.KeyExists("ftp", "publicip") {
  225. sysdb.Read("ftp", "publicip", &manualPublicIpEntry)
  226. }
  227. publicAddr = manualPublicIpEntry
  228. }
  229. }
  230. jsonString, _ := json.Marshal(ServerStatus{
  231. Enabled: enabled,
  232. Port: serverPort,
  233. AllowUPNP: *allow_upnp,
  234. UPNPEnabled: enableUPnP,
  235. FTPUpnpEnabled: ftpUpnp,
  236. PublicAddr: publicAddr,
  237. UserGroups: userGroups,
  238. PassiveMode: forcePassiveMode,
  239. })
  240. common.SendJSONResponse(w, string(jsonString))
  241. }
  242. func storageFTPServerStart() error {
  243. if ftpServer != nil {
  244. //If the previous ftp server is not closed, close it and open a new one
  245. if ftpServer.UPNPEnabled && UPNP != nil {
  246. UPNP.ClosePort(ftpServer.Port)
  247. }
  248. ftpServer.Close()
  249. }
  250. //Load new server config from database
  251. serverPort := int(21)
  252. if sysdb.KeyExists("ftp", "port") {
  253. sysdb.Read("ftp", "port", &serverPort)
  254. }
  255. enableUPnP := false
  256. if sysdb.KeyExists("ftp", "upnp") {
  257. sysdb.Read("ftp", "upnp", &enableUPnP)
  258. }
  259. forcePassiveMode := false
  260. sysdb.Read("ftp", "passive", &forcePassiveMode)
  261. //Create a new FTP Handler
  262. passiveModeIP := ""
  263. if *allow_upnp && enableUPnP {
  264. //Using External IP address from the UPnP router reply
  265. externalIP := UPNP.ExternalIP
  266. if externalIP != "" {
  267. passiveModeIP = externalIP
  268. }
  269. } else if forcePassiveMode {
  270. //Not allowing upnp but still use passive mode (aka manual port forward)
  271. externalIP := ""
  272. if sysdb.KeyExists("ftp", "publicip") {
  273. sysdb.Read("ftp", "publicip", &externalIP)
  274. }
  275. passiveModeIP = externalIP
  276. }
  277. h, err := ftp.NewFTPHandler(userHandler, *host_name, serverPort, *tmp_directory, passiveModeIP)
  278. if err != nil {
  279. return err
  280. }
  281. h.Start()
  282. ftpServer = h
  283. if *allow_upnp {
  284. if enableUPnP {
  285. if UPNP == nil {
  286. return errors.New("UPnP did not started correctly on this host. Ignore this option")
  287. } else {
  288. //Forward the port
  289. err := UPNP.ForwardPort(ftpServer.Port, *host_name+" FTP Server")
  290. if err != nil {
  291. log.Println("Failed to start FTP Server UPnP: ", err)
  292. ftpServer.UPNPEnabled = false
  293. return err
  294. } else {
  295. //Forward other data ports
  296. UPNP.ForwardPort(ftpServer.Port+1, *host_name+" FTP Data 1")
  297. UPNP.ForwardPort(ftpServer.Port+2, *host_name+" FTP Data 2")
  298. ftpServer.UPNPEnabled = true
  299. }
  300. return nil
  301. }
  302. } else {
  303. //UPNP disabled
  304. if UPNP == nil {
  305. return errors.New("UPnP did not started correctly on this host. Ignore this option")
  306. } else {
  307. UPNP.ClosePort(ftpServer.Port)
  308. UPNP.ClosePort(ftpServer.Port + 1)
  309. UPNP.ClosePort(ftpServer.Port + 2)
  310. ftpServer.UPNPEnabled = false
  311. }
  312. }
  313. }
  314. return nil
  315. }