Fix #202
This commit is contained in:
@@ -377,6 +377,7 @@ A built-in HTTP server listens on port `8000` to modify the state of the contain
|
|||||||
|
|
||||||
- `http://<your-docker-host-ip>:8000/openvpn/actions/restart` restarts the openvpn process
|
- `http://<your-docker-host-ip>:8000/openvpn/actions/restart` restarts the openvpn process
|
||||||
- `http://<your-docker-host-ip>:8000/unbound/actions/restart` re-downloads the DNS files (crypto and block lists) and restarts the unbound process
|
- `http://<your-docker-host-ip>:8000/unbound/actions/restart` re-downloads the DNS files (crypto and block lists) and restarts the unbound process
|
||||||
|
- `http://<your-docker-host-ip>:8000/openvpn/portforwarded` to get your port forwarded as JSON. You can use **jq** to parse JSON on linux.
|
||||||
|
|
||||||
## Development and contributing
|
## Development and contributing
|
||||||
|
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ func _main(background context.Context, args []string) int {
|
|||||||
ovpnConf, firewallConf, logger, client, fileManager, streamMerger, fatalOnError)
|
ovpnConf, firewallConf, logger, client, fileManager, streamMerger, fatalOnError)
|
||||||
restartOpenvpn := openvpnLooper.Restart
|
restartOpenvpn := openvpnLooper.Restart
|
||||||
portForward := openvpnLooper.PortForward
|
portForward := openvpnLooper.PortForward
|
||||||
|
getPortForwarded := openvpnLooper.GetPortForwarded
|
||||||
// wait for restartOpenvpn
|
// wait for restartOpenvpn
|
||||||
go openvpnLooper.Run(ctx, wg)
|
go openvpnLooper.Run(ctx, wg)
|
||||||
|
|
||||||
@@ -176,7 +177,7 @@ func _main(background context.Context, args []string) int {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
httpServer := server.New("0.0.0.0:8000", logger, restartOpenvpn, restartUnbound)
|
httpServer := server.New("0.0.0.0:8000", logger, restartOpenvpn, restartUnbound, getPortForwarded)
|
||||||
go httpServer.Run(ctx, wg)
|
go httpServer.Run(ctx, wg)
|
||||||
|
|
||||||
// Start openvpn for the first time
|
// Start openvpn for the first time
|
||||||
|
|||||||
@@ -23,13 +23,16 @@ type Looper interface {
|
|||||||
PortForward()
|
PortForward()
|
||||||
GetSettings() (settings settings.OpenVPN)
|
GetSettings() (settings settings.OpenVPN)
|
||||||
SetSettings(settings settings.OpenVPN)
|
SetSettings(settings settings.OpenVPN)
|
||||||
|
GetPortForwarded() (portForwarded uint16)
|
||||||
}
|
}
|
||||||
|
|
||||||
type looper struct {
|
type looper struct {
|
||||||
// Variable parameters
|
// Variable parameters
|
||||||
provider models.VPNProvider
|
provider models.VPNProvider
|
||||||
settings settings.OpenVPN
|
settings settings.OpenVPN
|
||||||
settingsMutex sync.RWMutex
|
settingsMutex sync.RWMutex
|
||||||
|
portForwarded uint16
|
||||||
|
portForwardedMutex sync.RWMutex
|
||||||
// Fixed parameters
|
// Fixed parameters
|
||||||
uid int
|
uid int
|
||||||
gid int
|
gid int
|
||||||
@@ -187,11 +190,14 @@ func (l *looper) portForward(ctx context.Context, providerConf provider.Provider
|
|||||||
port, err = providerConf.GetPortForward(client)
|
port, err = providerConf.GetPortForward(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
l.logger.Info("port forwarded is %d", port)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.logger.Info("port forwarded is %d", port)
|
||||||
|
l.portForwardedMutex.Lock()
|
||||||
|
l.portForwarded = port
|
||||||
|
l.portForwardedMutex.Unlock()
|
||||||
|
|
||||||
filepath := settings.Provider.PortForwarding.Filepath
|
filepath := settings.Provider.PortForwarding.Filepath
|
||||||
l.logger.Info("writing forwarded port to %s", filepath)
|
l.logger.Info("writing forwarded port to %s", filepath)
|
||||||
err = l.fileManager.WriteLinesToFile(
|
err = l.fileManager.WriteLinesToFile(
|
||||||
@@ -206,3 +212,9 @@ func (l *looper) portForward(ctx context.Context, providerConf provider.Provider
|
|||||||
l.logger.Error(err)
|
l.logger.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *looper) GetPortForwarded() (portForwarded uint16) {
|
||||||
|
l.portForwardedMutex.RLock()
|
||||||
|
defer l.portForwardedMutex.RUnlock()
|
||||||
|
return l.portForwarded
|
||||||
|
}
|
||||||
|
|||||||
23
internal/server/openvpn.go
Normal file
23
internal/server/openvpn.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *server) handleGetPortForwarded(w http.ResponseWriter) {
|
||||||
|
port := s.getPortForwarded()
|
||||||
|
data, err := json.Marshal(struct {
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
}{port})
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Warn(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write(data); err != nil {
|
||||||
|
s.logger.Warn(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -15,18 +15,21 @@ type Server interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
address string
|
address string
|
||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
restartOpenvpn func()
|
restartOpenvpn func()
|
||||||
restartUnbound func()
|
restartUnbound func()
|
||||||
|
getPortForwarded func() uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(address string, logger logging.Logger, restartOpenvpn, restartUnbound func()) Server {
|
func New(address string, logger logging.Logger, restartOpenvpn, restartUnbound func(),
|
||||||
|
getPortForwarded func() uint16) Server {
|
||||||
return &server{
|
return &server{
|
||||||
address: address,
|
address: address,
|
||||||
logger: logger.WithPrefix("http server: "),
|
logger: logger.WithPrefix("http server: "),
|
||||||
restartOpenvpn: restartOpenvpn,
|
restartOpenvpn: restartOpenvpn,
|
||||||
restartUnbound: restartUnbound,
|
restartUnbound: restartUnbound,
|
||||||
|
getPortForwarded: getPortForwarded,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +64,8 @@ func (s *server) makeHandler() http.HandlerFunc {
|
|||||||
s.restartOpenvpn()
|
s.restartOpenvpn()
|
||||||
case "/unbound/actions/restart":
|
case "/unbound/actions/restart":
|
||||||
s.restartUnbound()
|
s.restartUnbound()
|
||||||
|
case "/openvpn/portforwarded":
|
||||||
|
s.handleGetPortForwarded(w)
|
||||||
default:
|
default:
|
||||||
routeDoesNotExist(s.logger, w, r)
|
routeDoesNotExist(s.logger, w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user