fix: start ipsec tun not log.

This commit is contained in:
Daniel Ding
2024-06-22 21:42:03 +08:00
parent 97099b4881
commit 94f7e32992
9 changed files with 54 additions and 26 deletions

View File

@@ -1,9 +1,10 @@
package libol package libol
import ( import (
"github.com/xtaci/kcp-go/v5"
"net" "net"
"time" "time"
"github.com/xtaci/kcp-go/v5"
) )
type KcpConfig struct { type KcpConfig struct {
@@ -91,7 +92,7 @@ func (k *KcpServer) Accept() {
MinInt: 5 * time.Second, MinInt: 5 * time.Second,
MaxInt: 30 * time.Second, MaxInt: 30 * time.Second,
} }
promise.Done(func() error { promise.Do(func() error {
if err := k.Listen(); err != nil { if err := k.Listen(); err != nil {
Warn("KcpServer.Accept: %s", err) Warn("KcpServer.Accept: %s", err)
return err return err

View File

@@ -27,7 +27,7 @@ func NewPromiseAlways() *Promise {
} }
} }
func (p *Promise) Done(call func() error) { func (p *Promise) Do(call func() error) {
for { for {
p.Count++ p.Count++
if p.MaxTry > 0 && p.Count > p.MaxTry { if p.MaxTry > 0 && p.Count > p.MaxTry {
@@ -45,6 +45,13 @@ func (p *Promise) Done(call func() error) {
func (p *Promise) Go(call func() error) { func (p *Promise) Go(call func() error) {
Go(func() { Go(func() {
p.Done(call) p.Do(call)
})
}
func (p *Promise) Goto(call func() error, close func()) {
Go(func() {
p.Do(call)
close()
}) })
} }

View File

@@ -66,7 +66,7 @@ func (t *TcpServer) Accept() {
MinInt: 5 * time.Second, MinInt: 5 * time.Second,
MaxInt: 30 * time.Second, MaxInt: 30 * time.Second,
} }
promise.Done(func() error { promise.Do(func() error {
if err := t.Listen(); err != nil { if err := t.Listen(); err != nil {
Warn("TcpServer.Accept: %s", err) Warn("TcpServer.Accept: %s", err)
return err return err

View File

@@ -63,7 +63,7 @@ func (k *UdpServer) Accept() {
MinInt: 5 * time.Second, MinInt: 5 * time.Second,
MaxInt: 30 * time.Second, MaxInt: 30 * time.Second,
} }
promise.Done(func() error { promise.Do(func() error {
if err := k.Listen(); err != nil { if err := k.Listen(); err != nil {
Warn("UdpServer.Accept: %s", err) Warn("UdpServer.Accept: %s", err)
return err return err

View File

@@ -3,11 +3,12 @@ package libol
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"golang.org/x/net/websocket"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"time" "time"
"golang.org/x/net/websocket"
) )
type wsConn struct { type wsConn struct {
@@ -101,7 +102,7 @@ func (t *WebServer) Accept() {
MinInt: 5 * time.Second, MinInt: 5 * time.Second,
MaxInt: 30 * time.Second, MaxInt: 30 * time.Second,
} }
promise.Done(func() error { promise.Do(func() error {
if t.webCfg.Cert == nil { if t.webCfg.Cert == nil {
if err := t.listener.ListenAndServe(); err != nil { if err := t.listener.ListenAndServe(); err != nil {
Error("WebServer.Accept on %s: %s", t.address, err) Error("WebServer.Accept on %s: %s", t.address, err)

View File

@@ -1,11 +1,12 @@
package proxy package proxy
import ( import (
"github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"io" "io"
"net" "net"
"time" "time"
"github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
) )
type TcpProxy struct { type TcpProxy struct {
@@ -62,7 +63,7 @@ func (t *TcpProxy) Start() {
MaxInt: time.Minute, MaxInt: time.Minute,
MinInt: time.Second * 10, MinInt: time.Second * 10,
} }
promise.Done(func() error { promise.Do(func() error {
var err error var err error
listen, err = net.Listen("tcp", t.listen) listen, err = net.Listen("tcp", t.listen)
if err != nil { if err != nil {

View File

@@ -172,7 +172,7 @@ func (h *Http) Start() {
MaxInt: time.Minute, MaxInt: time.Minute,
MinInt: time.Second * 10, MinInt: time.Second * 10,
} }
promise.Done(func() error { promise.Go(func() error {
if h.keyFile == "" || h.crtFile == "" { if h.keyFile == "" || h.crtFile == "" {
if err := h.server.ListenAndServe(); err != nil { if err := h.server.ListenAndServe(); err != nil {
libol.Error("Http.Start on %s: %s", h.listen, err) libol.Error("Http.Start on %s: %s", h.listen, err)

View File

@@ -3,6 +3,7 @@ package cswitch
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"text/template" "text/template"
"github.com/luscis/openlan/pkg/api" "github.com/luscis/openlan/pkg/api"
@@ -11,6 +12,12 @@ import (
"github.com/luscis/openlan/pkg/schema" "github.com/luscis/openlan/pkg/schema"
) )
const (
IPSecBin = "/usr/sbin/ipsec"
IPSecEtcDir = "/etc/ipsec.d"
IPSecLogDir = "/var/openlan/ipsec"
)
type IPSecWorker struct { type IPSecWorker struct {
*WorkerImpl *WorkerImpl
spec *co.IPSecSpecifies spec *co.IPSecSpecifies
@@ -94,10 +101,13 @@ conn {{ .Name }}-c1
func (w *IPSecWorker) Initialize() { func (w *IPSecWorker) Initialize() {
w.out.Info("IPSecWorker.Initialize") w.out.Info("IPSecWorker.Initialize")
if err := os.Mkdir(IPSecLogDir, 0600); err != nil {
w.out.Warn("IPSecWorker.Initialize %s", err)
}
} }
func (w *IPSecWorker) saveSec(name, tmpl string, data interface{}) error { func (w *IPSecWorker) saveSec(name, tmpl string, data interface{}) error {
file := fmt.Sprintf("/etc/ipsec.d/%s", name) file := fmt.Sprintf("%s/%s", IPSecEtcDir, name)
out, err := libol.CreateFile(file) out, err := libol.CreateFile(file)
if err != nil || out == nil { if err != nil || out == nil {
return err return err
@@ -114,14 +124,22 @@ func (w *IPSecWorker) saveSec(name, tmpl string, data interface{}) error {
} }
func (w *IPSecWorker) startConn(name string) { func (w *IPSecWorker) startConn(name string) {
promise := libol.NewPromise() logFile := fmt.Sprintf("%s/%s.log", IPSecLogDir, name)
promise.Go(func() error { logto, err := libol.CreateFile(logFile)
if out, err := libol.Exec("ipsec", "auto", "--start", "--asynchronous", name); err != nil { if err != nil {
w.out.Warn("IPSecWorker.startConn: %v %s", out, err) w.out.Warn("IPSecWorker.startConn %s", err)
return err return
}
libol.Go(func() {
defer logto.Close()
cmd := exec.Command(IPSecBin, "auto", "--start", name)
cmd.Stdout = logto
cmd.Stderr = logto
if err := cmd.Run(); err != nil {
w.out.Warn("IPSecWorker.startConn: %s", err)
return
} }
w.out.Info("IPSecWorker.startConn: %v success", name) w.out.Info("IPSecWorker.startConn: %v success", name)
return nil
}) })
} }
@@ -182,8 +200,8 @@ func (w *IPSecWorker) removeTunnel(tun *co.IPSecTunnel) error {
} else if tun.Transport == "gre" { } else if tun.Transport == "gre" {
libol.Exec("ipsec", "auto", "--delete", "--asynchronous", name+"-c1") libol.Exec("ipsec", "auto", "--delete", "--asynchronous", name+"-c1")
} }
cfile := fmt.Sprintf("/etc/ipsec.d/%s.conf", name) cfile := fmt.Sprintf("%s/%s.conf", IPSecEtcDir, name)
sfile := fmt.Sprintf("/etc/ipsec.d/%s.secrets", name) sfile := fmt.Sprintf("%s/%s.secrets", IPSecEtcDir, name)
if err := libol.FileExist(cfile); err == nil { if err := libol.FileExist(cfile); err == nil {
if err := os.Remove(cfile); err != nil { if err := os.Remove(cfile); err != nil {

View File

@@ -18,8 +18,8 @@ import (
) )
const ( const (
OpenVPNBin = "openvpn" OpenVPNBin = "openvpn"
DefaultCurDir = "/var/openlan/openvpn/default" VPNCurDir = "/var/openlan/openvpn/default"
) )
type OpenVPNData struct { type OpenVPNData struct {
@@ -193,7 +193,7 @@ func (o *OpenVPN) Path() string {
func (o *OpenVPN) Directory() string { func (o *OpenVPN) Directory() string {
if o.Cfg == nil { if o.Cfg == nil {
return DefaultCurDir return VPNCurDir
} }
return o.Cfg.Directory return o.Cfg.Directory
} }
@@ -301,14 +301,14 @@ func (o *OpenVPN) Pid(full bool) string {
func (o *OpenVPN) DirectoryClientConfig() string { func (o *OpenVPN) DirectoryClientConfig() string {
if o.Cfg == nil { if o.Cfg == nil {
return path.Join(DefaultCurDir, "ccd") return path.Join(VPNCurDir, "ccd")
} }
return path.Join(o.Cfg.Directory, "ccd") return path.Join(o.Cfg.Directory, "ccd")
} }
func (o *OpenVPN) ClientIvplatDir() string { func (o *OpenVPN) ClientIvplatDir() string {
if o.Cfg == nil { if o.Cfg == nil {
return DefaultCurDir return VPNCurDir
} }
return o.Cfg.Directory return o.Cfg.Directory
} }