vpn_handlers.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/vpn"
  6. )
  7. func (s *Server) handleOverallVPNStatus(w http.ResponseWriter, r *http.Request) {
  8. status := s.vpnMgr.GetOverallStatus()
  9. writeJSON(w, http.StatusOK, status)
  10. }
  11. // ======================== PPPoE Multi-Instance Handlers ========================
  12. func (s *Server) handleListPPPoE(w http.ResponseWriter, r *http.Request) {
  13. list := s.vpnMgr.PPPoE.List()
  14. writeJSON(w, http.StatusOK, list)
  15. }
  16. func (s *Server) handleSavePPPoE(w http.ResponseWriter, r *http.Request) {
  17. var inst vpn.PPPoEInstance
  18. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  19. writeError(w, http.StatusBadRequest, "invalid request body")
  20. return
  21. }
  22. if err := s.vpnMgr.PPPoE.Save(inst); err != nil {
  23. writeError(w, http.StatusInternalServerError, err.Error())
  24. return
  25. }
  26. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
  27. }
  28. func (s *Server) handleDeletePPPoE(w http.ResponseWriter, r *http.Request) {
  29. name := r.PathValue("name")
  30. if err := s.vpnMgr.PPPoE.Delete(name); err != nil {
  31. writeError(w, http.StatusInternalServerError, err.Error())
  32. return
  33. }
  34. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  35. }
  36. func (s *Server) handleSetPPPoEState(w http.ResponseWriter, r *http.Request) {
  37. name := r.PathValue("name")
  38. var req struct {
  39. Enabled bool `json:"enabled"`
  40. }
  41. _ = json.NewDecoder(r.Body).Decode(&req)
  42. if err := s.vpnMgr.PPPoE.SetEnabled(name, req.Enabled); err != nil {
  43. writeError(w, http.StatusInternalServerError, err.Error())
  44. return
  45. }
  46. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
  47. }
  48. func (s *Server) handleConnectPPPoE(w http.ResponseWriter, r *http.Request) {
  49. name := r.PathValue("name")
  50. if err := s.vpnMgr.PPPoE.Connect(name); err != nil {
  51. writeError(w, http.StatusInternalServerError, err.Error())
  52. return
  53. }
  54. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  55. }
  56. func (s *Server) handleDisconnectPPPoE(w http.ResponseWriter, r *http.Request) {
  57. name := r.PathValue("name")
  58. if err := s.vpnMgr.PPPoE.Disconnect(name); err != nil {
  59. writeError(w, http.StatusInternalServerError, err.Error())
  60. return
  61. }
  62. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  63. }
  64. // ======================== WireGuard Multi-Interface & Peer Handlers ========================
  65. func (s *Server) handleListWireGuardInterfaces(w http.ResponseWriter, r *http.Request) {
  66. ifaces := s.vpnMgr.WireGuard.ListInterfaces()
  67. writeJSON(w, http.StatusOK, ifaces)
  68. }
  69. func (s *Server) handleSaveWireGuardInterface(w http.ResponseWriter, r *http.Request) {
  70. var iface vpn.WireGuardInterface
  71. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  72. writeError(w, http.StatusBadRequest, "invalid request body")
  73. return
  74. }
  75. if err := s.vpnMgr.WireGuard.SaveInterface(iface); err != nil {
  76. writeError(w, http.StatusInternalServerError, err.Error())
  77. return
  78. }
  79. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": iface.Name})
  80. }
  81. func (s *Server) handleDeleteWireGuardInterface(w http.ResponseWriter, r *http.Request) {
  82. name := r.PathValue("name")
  83. if err := s.vpnMgr.WireGuard.DeleteInterface(name); err != nil {
  84. writeError(w, http.StatusInternalServerError, err.Error())
  85. return
  86. }
  87. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  88. }
  89. func (s *Server) handleSetWireGuardInterfaceState(w http.ResponseWriter, r *http.Request) {
  90. name := r.PathValue("name")
  91. var req struct {
  92. Enabled bool `json:"enabled"`
  93. }
  94. _ = json.NewDecoder(r.Body).Decode(&req)
  95. if err := s.vpnMgr.WireGuard.SetInterfaceEnabled(name, req.Enabled); err != nil {
  96. writeError(w, http.StatusInternalServerError, err.Error())
  97. return
  98. }
  99. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
  100. }
  101. func (s *Server) handleStartWireGuardInterface(w http.ResponseWriter, r *http.Request) {
  102. name := r.PathValue("name")
  103. if err := s.vpnMgr.WireGuard.StartInterface(name); err != nil {
  104. writeError(w, http.StatusInternalServerError, err.Error())
  105. return
  106. }
  107. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  108. }
  109. func (s *Server) handleStopWireGuardInterface(w http.ResponseWriter, r *http.Request) {
  110. name := r.PathValue("name")
  111. if err := s.vpnMgr.WireGuard.StopInterface(name); err != nil {
  112. writeError(w, http.StatusInternalServerError, err.Error())
  113. return
  114. }
  115. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  116. }
  117. func (s *Server) handleListWireGuardPeers(w http.ResponseWriter, r *http.Request) {
  118. ifaceFilter := r.URL.Query().Get("interface")
  119. peers := s.vpnMgr.WireGuard.ListPeers(ifaceFilter)
  120. writeJSON(w, http.StatusOK, peers)
  121. }
  122. func (s *Server) handleSaveWireGuardPeer(w http.ResponseWriter, r *http.Request) {
  123. var peer vpn.WireGuardPeer
  124. if err := json.NewDecoder(r.Body).Decode(&peer); err != nil || peer.PublicKey == "" || peer.Interface == "" {
  125. writeError(w, http.StatusBadRequest, "interface and public_key are required")
  126. return
  127. }
  128. if err := s.vpnMgr.WireGuard.SavePeer(peer); err != nil {
  129. writeError(w, http.StatusInternalServerError, err.Error())
  130. return
  131. }
  132. writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": peer.Interface, "public_key": peer.PublicKey})
  133. }
  134. func (s *Server) handleDeleteWireGuardPeer(w http.ResponseWriter, r *http.Request) {
  135. iface := r.PathValue("iface")
  136. pubkey := r.PathValue("pubkey")
  137. if err := s.vpnMgr.WireGuard.DeletePeer(iface, pubkey); err != nil {
  138. writeError(w, http.StatusInternalServerError, err.Error())
  139. return
  140. }
  141. writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": iface, "public_key": pubkey})
  142. }
  143. func (s *Server) handleWireGuardGenKeys(w http.ResponseWriter, r *http.Request) {
  144. priv, pub, err := s.vpnMgr.WireGuard.GenerateKeyPair()
  145. if err != nil {
  146. writeError(w, http.StatusInternalServerError, err.Error())
  147. return
  148. }
  149. writeJSON(w, http.StatusOK, map[string]string{
  150. "private_key": priv,
  151. "public_key": pub,
  152. })
  153. }
  154. // ======================== PPTP Multi-Instance Handlers ========================
  155. func (s *Server) handleListPPTP(w http.ResponseWriter, r *http.Request) {
  156. list := s.vpnMgr.PPTP.List()
  157. writeJSON(w, http.StatusOK, list)
  158. }
  159. func (s *Server) handleSavePPTP(w http.ResponseWriter, r *http.Request) {
  160. var inst vpn.PPTPInstance
  161. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  162. writeError(w, http.StatusBadRequest, "invalid request body")
  163. return
  164. }
  165. if err := s.vpnMgr.PPTP.Save(inst); err != nil {
  166. writeError(w, http.StatusInternalServerError, err.Error())
  167. return
  168. }
  169. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
  170. }
  171. func (s *Server) handleDeletePPTP(w http.ResponseWriter, r *http.Request) {
  172. name := r.PathValue("name")
  173. if err := s.vpnMgr.PPTP.Delete(name); err != nil {
  174. writeError(w, http.StatusInternalServerError, err.Error())
  175. return
  176. }
  177. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  178. }
  179. func (s *Server) handleSetPPTPState(w http.ResponseWriter, r *http.Request) {
  180. name := r.PathValue("name")
  181. var req struct {
  182. Enabled bool `json:"enabled"`
  183. }
  184. _ = json.NewDecoder(r.Body).Decode(&req)
  185. if err := s.vpnMgr.PPTP.SetEnabled(name, req.Enabled); err != nil {
  186. writeError(w, http.StatusInternalServerError, err.Error())
  187. return
  188. }
  189. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
  190. }
  191. func (s *Server) handleConnectPPTP(w http.ResponseWriter, r *http.Request) {
  192. name := r.PathValue("name")
  193. if err := s.vpnMgr.PPTP.Connect(name); err != nil {
  194. writeError(w, http.StatusInternalServerError, err.Error())
  195. return
  196. }
  197. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  198. }
  199. func (s *Server) handleDisconnectPPTP(w http.ResponseWriter, r *http.Request) {
  200. name := r.PathValue("name")
  201. if err := s.vpnMgr.PPTP.Disconnect(name); err != nil {
  202. writeError(w, http.StatusInternalServerError, err.Error())
  203. return
  204. }
  205. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  206. }
  207. // ======================== L2TP Multi-Instance Handlers ========================
  208. func (s *Server) handleListL2TP(w http.ResponseWriter, r *http.Request) {
  209. list := s.vpnMgr.L2TP.List()
  210. writeJSON(w, http.StatusOK, list)
  211. }
  212. func (s *Server) handleSaveL2TP(w http.ResponseWriter, r *http.Request) {
  213. var inst vpn.L2TPInstance
  214. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  215. writeError(w, http.StatusBadRequest, "invalid request body")
  216. return
  217. }
  218. if err := s.vpnMgr.L2TP.Save(inst); err != nil {
  219. writeError(w, http.StatusInternalServerError, err.Error())
  220. return
  221. }
  222. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
  223. }
  224. func (s *Server) handleDeleteL2TP(w http.ResponseWriter, r *http.Request) {
  225. name := r.PathValue("name")
  226. if err := s.vpnMgr.L2TP.Delete(name); err != nil {
  227. writeError(w, http.StatusInternalServerError, err.Error())
  228. return
  229. }
  230. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  231. }
  232. func (s *Server) handleSetL2TPState(w http.ResponseWriter, r *http.Request) {
  233. name := r.PathValue("name")
  234. var req struct {
  235. Enabled bool `json:"enabled"`
  236. }
  237. _ = json.NewDecoder(r.Body).Decode(&req)
  238. if err := s.vpnMgr.L2TP.SetEnabled(name, req.Enabled); err != nil {
  239. writeError(w, http.StatusInternalServerError, err.Error())
  240. return
  241. }
  242. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
  243. }
  244. func (s *Server) handleConnectL2TP(w http.ResponseWriter, r *http.Request) {
  245. name := r.PathValue("name")
  246. if err := s.vpnMgr.L2TP.Connect(name); err != nil {
  247. writeError(w, http.StatusInternalServerError, err.Error())
  248. return
  249. }
  250. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  251. }
  252. func (s *Server) handleDisconnectL2TP(w http.ResponseWriter, r *http.Request) {
  253. name := r.PathValue("name")
  254. if err := s.vpnMgr.L2TP.Disconnect(name); err != nil {
  255. writeError(w, http.StatusInternalServerError, err.Error())
  256. return
  257. }
  258. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  259. }
  260. // ======================== IPSec Multi-Tunnel Handlers ========================
  261. func (s *Server) handleListIPSec(w http.ResponseWriter, r *http.Request) {
  262. list := s.vpnMgr.IPSec.List()
  263. writeJSON(w, http.StatusOK, list)
  264. }
  265. func (s *Server) handleSaveIPSecTunnel(w http.ResponseWriter, r *http.Request) {
  266. var conn vpn.IPSecConnection
  267. if err := json.NewDecoder(r.Body).Decode(&conn); err != nil || conn.Name == "" {
  268. writeError(w, http.StatusBadRequest, "invalid request body")
  269. return
  270. }
  271. if err := s.vpnMgr.IPSec.SaveConnection(conn); err != nil {
  272. writeError(w, http.StatusInternalServerError, err.Error())
  273. return
  274. }
  275. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": conn.Name})
  276. }
  277. func (s *Server) handleDeleteIPSecTunnel(w http.ResponseWriter, r *http.Request) {
  278. name := r.PathValue("name")
  279. if err := s.vpnMgr.IPSec.DeleteConnection(name); err != nil {
  280. writeError(w, http.StatusInternalServerError, err.Error())
  281. return
  282. }
  283. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  284. }
  285. func (s *Server) handleSetIPSecState(w http.ResponseWriter, r *http.Request) {
  286. name := r.PathValue("name")
  287. var req struct {
  288. Enabled bool `json:"enabled"`
  289. }
  290. _ = json.NewDecoder(r.Body).Decode(&req)
  291. if err := s.vpnMgr.IPSec.SetEnabled(name, req.Enabled); err != nil {
  292. writeError(w, http.StatusInternalServerError, err.Error())
  293. return
  294. }
  295. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
  296. }
  297. func (s *Server) handleInitiateIPSecTunnel(w http.ResponseWriter, r *http.Request) {
  298. name := r.PathValue("name")
  299. if err := s.vpnMgr.IPSec.StartTunnel(name); err != nil {
  300. writeError(w, http.StatusInternalServerError, err.Error())
  301. return
  302. }
  303. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  304. }
  305. func (s *Server) handleTerminateIPSecTunnel(w http.ResponseWriter, r *http.Request) {
  306. name := r.PathValue("name")
  307. if err := s.vpnMgr.IPSec.StopTunnel(name); err != nil {
  308. writeError(w, http.StatusInternalServerError, err.Error())
  309. return
  310. }
  311. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  312. }
  313. // ======================== EoIP (Ethernet-over-IP) Handlers ========================
  314. func (s *Server) handleListEoIP(w http.ResponseWriter, r *http.Request) {
  315. list := s.vpnMgr.EoIP.List()
  316. writeJSON(w, http.StatusOK, list)
  317. }
  318. func (s *Server) handleSaveEoIP(w http.ResponseWriter, r *http.Request) {
  319. var inst vpn.EoIPInstance
  320. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  321. writeError(w, http.StatusBadRequest, "invalid request body")
  322. return
  323. }
  324. if err := s.vpnMgr.EoIP.Save(&inst); err != nil {
  325. writeError(w, http.StatusInternalServerError, err.Error())
  326. return
  327. }
  328. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": inst.ID, "name": inst.Name})
  329. }
  330. func (s *Server) handleDeleteEoIP(w http.ResponseWriter, r *http.Request) {
  331. id := r.PathValue("id")
  332. if err := s.vpnMgr.EoIP.Delete(id); err != nil {
  333. writeError(w, http.StatusInternalServerError, err.Error())
  334. return
  335. }
  336. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
  337. }
  338. func (s *Server) handleSetEoIPState(w http.ResponseWriter, r *http.Request) {
  339. id := r.PathValue("id")
  340. var req struct {
  341. Disabled bool `json:"disabled"`
  342. }
  343. _ = json.NewDecoder(r.Body).Decode(&req)
  344. if err := s.vpnMgr.EoIP.SetState(id, req.Disabled); err != nil {
  345. writeError(w, http.StatusInternalServerError, err.Error())
  346. return
  347. }
  348. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id, "disabled": req.Disabled})
  349. }
  350. // ======================== PPP Profiles Handlers ========================
  351. func (s *Server) handleListPPPProfiles(w http.ResponseWriter, r *http.Request) {
  352. list := s.vpnMgr.PPPServer.ListProfiles()
  353. writeJSON(w, http.StatusOK, list)
  354. }
  355. func (s *Server) handleSavePPPProfile(w http.ResponseWriter, r *http.Request) {
  356. var prof vpn.PPPProfile
  357. if err := json.NewDecoder(r.Body).Decode(&prof); err != nil {
  358. writeError(w, http.StatusBadRequest, "invalid request body")
  359. return
  360. }
  361. if err := s.vpnMgr.PPPServer.SaveProfile(&prof); err != nil {
  362. writeError(w, http.StatusInternalServerError, err.Error())
  363. return
  364. }
  365. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": prof.Name})
  366. }
  367. func (s *Server) handleDeletePPPProfile(w http.ResponseWriter, r *http.Request) {
  368. name := r.PathValue("name")
  369. if err := s.vpnMgr.PPPServer.DeleteProfile(name); err != nil {
  370. writeError(w, http.StatusInternalServerError, err.Error())
  371. return
  372. }
  373. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  374. }
  375. // ======================== PPP Secrets Handlers ========================
  376. func (s *Server) handleListPPPSecrets(w http.ResponseWriter, r *http.Request) {
  377. list := s.vpnMgr.PPPServer.ListSecrets()
  378. writeJSON(w, http.StatusOK, list)
  379. }
  380. func (s *Server) handleSavePPPSecret(w http.ResponseWriter, r *http.Request) {
  381. var sec vpn.PPPSecret
  382. if err := json.NewDecoder(r.Body).Decode(&sec); err != nil {
  383. writeError(w, http.StatusBadRequest, "invalid request body")
  384. return
  385. }
  386. if err := s.vpnMgr.PPPServer.SaveSecret(&sec); err != nil {
  387. writeError(w, http.StatusInternalServerError, err.Error())
  388. return
  389. }
  390. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": sec.ID, "name": sec.Name})
  391. }
  392. func (s *Server) handleSetPPPSecretState(w http.ResponseWriter, r *http.Request) {
  393. id := r.PathValue("id")
  394. var req struct {
  395. Disabled bool `json:"disabled"`
  396. }
  397. _ = json.NewDecoder(r.Body).Decode(&req)
  398. if err := s.vpnMgr.PPPServer.SetSecretState(id, req.Disabled); err != nil {
  399. writeError(w, http.StatusInternalServerError, err.Error())
  400. return
  401. }
  402. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id, "disabled": req.Disabled})
  403. }
  404. func (s *Server) handleDeletePPPSecret(w http.ResponseWriter, r *http.Request) {
  405. id := r.PathValue("id")
  406. if err := s.vpnMgr.PPPServer.DeleteSecret(id); err != nil {
  407. writeError(w, http.StatusInternalServerError, err.Error())
  408. return
  409. }
  410. writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
  411. }
  412. // ======================== Active PPP Sessions Handlers ========================
  413. func (s *Server) handleListPPPSessions(w http.ResponseWriter, r *http.Request) {
  414. list := s.vpnMgr.PPPServer.ListActiveSessions()
  415. writeJSON(w, http.StatusOK, list)
  416. }
  417. func (s *Server) handleDisconnectPPPSession(w http.ResponseWriter, r *http.Request) {
  418. iface := r.PathValue("iface")
  419. if err := s.vpnMgr.PPPServer.DisconnectSession(iface); err != nil {
  420. writeError(w, http.StatusInternalServerError, err.Error())
  421. return
  422. }
  423. writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": iface})
  424. }
  425. // ======================== VPN Servers Configuration Handlers ========================
  426. func (s *Server) handleGetPPPoEServer(w http.ResponseWriter, r *http.Request) {
  427. cfg := s.vpnMgr.PPPServer.GetPPPoEServer()
  428. writeJSON(w, http.StatusOK, cfg)
  429. }
  430. func (s *Server) handleSetPPPoEServer(w http.ResponseWriter, r *http.Request) {
  431. var cfg vpn.PPPoEServerConfig
  432. if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
  433. writeError(w, http.StatusBadRequest, "invalid request body")
  434. return
  435. }
  436. if err := s.vpnMgr.PPPServer.SetPPPoEServer(cfg); err != nil {
  437. writeError(w, http.StatusInternalServerError, err.Error())
  438. return
  439. }
  440. writeJSON(w, http.StatusOK, map[string]any{"success": true})
  441. }
  442. func (s *Server) handleGetPPTPServer(w http.ResponseWriter, r *http.Request) {
  443. cfg := s.vpnMgr.PPPServer.GetPPTPServer()
  444. writeJSON(w, http.StatusOK, cfg)
  445. }
  446. func (s *Server) handleSetPPTPServer(w http.ResponseWriter, r *http.Request) {
  447. var cfg vpn.PPTPServerConfig
  448. if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
  449. writeError(w, http.StatusBadRequest, "invalid request body")
  450. return
  451. }
  452. if err := s.vpnMgr.PPPServer.SetPPTPServer(cfg); err != nil {
  453. writeError(w, http.StatusInternalServerError, err.Error())
  454. return
  455. }
  456. writeJSON(w, http.StatusOK, map[string]any{"success": true})
  457. }
  458. func (s *Server) handleGetL2TPServer(w http.ResponseWriter, r *http.Request) {
  459. cfg := s.vpnMgr.PPPServer.GetL2TPServer()
  460. writeJSON(w, http.StatusOK, cfg)
  461. }
  462. func (s *Server) handleSetL2TPServer(w http.ResponseWriter, r *http.Request) {
  463. var cfg vpn.L2TPServerConfig
  464. if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
  465. writeError(w, http.StatusBadRequest, "invalid request body")
  466. return
  467. }
  468. if err := s.vpnMgr.PPPServer.SetL2TPServer(cfg); err != nil {
  469. writeError(w, http.StatusInternalServerError, err.Error())
  470. return
  471. }
  472. writeJSON(w, http.StatusOK, map[string]any{"success": true})
  473. }
  474. func (s *Server) handleGetIPSecServer(w http.ResponseWriter, r *http.Request) {
  475. cfg := s.vpnMgr.PPPServer.GetIPSecServer()
  476. writeJSON(w, http.StatusOK, cfg)
  477. }
  478. func (s *Server) handleSetIPSecServer(w http.ResponseWriter, r *http.Request) {
  479. var cfg vpn.IPSecServerConfig
  480. if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
  481. writeError(w, http.StatusBadRequest, "invalid request body")
  482. return
  483. }
  484. if err := s.vpnMgr.PPPServer.SetIPSecServer(cfg); err != nil {
  485. writeError(w, http.StatusInternalServerError, err.Error())
  486. return
  487. }
  488. writeJSON(w, http.StatusOK, map[string]any{"success": true})
  489. }
  490. // WireGuard Client Config Generator
  491. func (s *Server) handleGenerateWGClientConfig(w http.ResponseWriter, r *http.Request) {
  492. var req struct {
  493. Interface string `json:"interface"`
  494. Address string `json:"address"`
  495. PrivKey string `json:"private_key"`
  496. Endpoint string `json:"endpoint"`
  497. DNS string `json:"dns"`
  498. }
  499. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Interface == "" {
  500. writeError(w, http.StatusBadRequest, "interface is required")
  501. return
  502. }
  503. if req.PrivKey == "" {
  504. priv, _, err := s.vpnMgr.WireGuard.GenerateKeyPair()
  505. if err == nil {
  506. req.PrivKey = priv
  507. }
  508. }
  509. conf, err := s.vpnMgr.WireGuard.GenerateClientConfig(req.Interface, req.PrivKey, req.Address, req.Endpoint, req.DNS)
  510. if err != nil {
  511. writeError(w, http.StatusInternalServerError, err.Error())
  512. return
  513. }
  514. writeJSON(w, http.StatusOK, map[string]any{"success": true, "config": conf, "client_private_key": req.PrivKey})
  515. }