mirror of
https://github.com/luscis/openlan.git
synced 2025-10-05 08:36:59 +08:00
clone from danieldin95
This commit is contained in:
51
pkg/api/network.go
Executable file
51
pkg/api/network.go
Executable file
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/luscis/openlan/pkg/cache"
|
||||
"github.com/luscis/openlan/pkg/models"
|
||||
"github.com/luscis/openlan/pkg/schema"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Network struct {
|
||||
}
|
||||
|
||||
func (h Network) Router(router *mux.Router) {
|
||||
router.HandleFunc("/api/network", h.List).Methods("GET")
|
||||
router.HandleFunc("/api/network/{id}", h.Get).Methods("GET")
|
||||
router.HandleFunc("/get/network/{id}/{ie}.ovpn", h.Profile).Methods("GET")
|
||||
}
|
||||
|
||||
func (h Network) List(w http.ResponseWriter, r *http.Request) {
|
||||
nets := make([]schema.Network, 0, 1024)
|
||||
for u := range cache.Network.List() {
|
||||
if u == nil {
|
||||
break
|
||||
}
|
||||
nets = append(nets, models.NewNetworkSchema(u))
|
||||
}
|
||||
ResponseJson(w, nets)
|
||||
}
|
||||
|
||||
func (h Network) Get(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
net := cache.Network.Get(vars["id"])
|
||||
if net != nil {
|
||||
ResponseJson(w, models.NewNetworkSchema(net))
|
||||
} else {
|
||||
http.Error(w, vars["id"], http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func (h Network) Profile(w http.ResponseWriter, r *http.Request) {
|
||||
server := strings.SplitN(r.Host, ":", 2)[0]
|
||||
vars := mux.Vars(r)
|
||||
data, err := cache.VPNClient.GetClientProfile(vars["id"], vars["ie"], server)
|
||||
if err == nil {
|
||||
_, _ = w.Write([]byte(data))
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user