Files
openlan/pkg/access/access.go
2025-04-21 10:45:08 +08:00

151 lines
2.7 KiB
Go
Executable File

package access
import (
"runtime"
"github.com/luscis/openlan/pkg/access/http"
"github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/models"
"github.com/luscis/openlan/pkg/network"
)
type Acceser interface {
Addr() string
IfName() string
IfAddr() string
Client() libol.SocketClient
Device() network.Taper
Status() libol.SocketStatus
UpTime() int64
UUID() string
Protocol() string
User() string
Record() map[string]int64
Tenant() string
Alias() string
Config() *config.Access
Network() *models.Network
}
type MixAccess struct {
uuid string
worker *Worker
config *config.Access
out *libol.SubLogger
http *http.Http
}
func NewMixAccess(config *config.Access) MixAccess {
return MixAccess{
worker: NewWorker(config),
config: config,
out: libol.NewSubLogger(config.Id()),
}
}
func (p *MixAccess) Initialize() {
libol.Info("MixAccess.Initialize")
p.worker.SetUUID(p.UUID())
p.worker.Initialize()
if p.config.Http != nil {
p.http = http.NewHttp(p)
}
}
func (p *MixAccess) Start() {
p.out.Info("MixAccess.Start %s", runtime.GOOS)
if p.config.PProf != "" {
f := libol.PProf{Listen: p.config.PProf}
f.Start()
}
p.worker.Start()
}
func (p *MixAccess) Stop() {
defer libol.Catch("MixAccess.Stop")
if p.http != nil {
p.http.Shutdown()
}
p.worker.Stop()
}
func (p *MixAccess) UUID() string {
if p.uuid == "" {
p.uuid = libol.GenString(13)
}
return p.uuid
}
func (p *MixAccess) Status() libol.SocketStatus {
client := p.Client()
if client == nil {
return 0
}
return client.Status()
}
func (p *MixAccess) Addr() string {
return p.config.Connection
}
func (p *MixAccess) IfName() string {
device := p.Device()
if device == nil {
return ""
}
return device.Name()
}
func (p *MixAccess) Client() libol.SocketClient {
if p.worker.conWorker == nil {
return nil
}
return p.worker.conWorker.client
}
func (p *MixAccess) Device() network.Taper {
if p.worker.tapWorker == nil {
return nil
}
return p.worker.tapWorker.device
}
func (p *MixAccess) UpTime() int64 {
return p.worker.UpTime()
}
func (p *MixAccess) IfAddr() string {
return p.worker.ifAddr
}
func (p *MixAccess) Tenant() string {
return p.config.Network
}
func (p *MixAccess) User() string {
return p.config.Username
}
func (p *MixAccess) Alias() string {
return p.config.Alias
}
func (p *MixAccess) Record() map[string]int64 {
rt := p.worker.conWorker.record
// TODO padding data from tapWorker
return rt.Data()
}
func (p *MixAccess) Config() *config.Access {
return p.config
}
func (p *MixAccess) Network() *models.Network {
return p.worker.network
}
func (p *MixAccess) Protocol() string {
return p.config.Protocol
}