Skip to content

Commit

Permalink
refactored most stuffs
Browse files Browse the repository at this point in the history
:o
  • Loading branch information
zyxkad committed Aug 11, 2024
1 parent 3c20fd8 commit 173aced
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 426 deletions.
2 changes: 1 addition & 1 deletion api/bmclapi/hijacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package main
package bmclapi

import (
"context"
Expand Down
21 changes: 19 additions & 2 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ type Cluster struct {

func NewCluster(
opts config.ClusterOptions, gcfg config.ClusterGeneralConfig,
storageManager *storage.Manager, storages []int,
storageManager *storage.Manager,
statManager *StatManager,
) (cr *Cluster) {
storages := make([]int, len(opts.Storages))
for i, name := range opts.Storages {
storages[i] = storageManager.GetIndex(name)
}
cr = &Cluster{
opts: opts,
gcfg: gcfg,
Expand Down Expand Up @@ -120,10 +124,23 @@ func (cr *Cluster) AcceptHost(host string) bool {
return false
}

func (cr *Cluster) Options() *config.ClusterOptions {
return &cr.opts
}

func (cr *Cluster) GeneralConfig() *config.ClusterGeneralConfig {
return &cr.gcfg
}

// Init do setup on the cluster
// Init should only be called once during the cluster's whole life
// The context passed in only affect the logical of Init method
func (cr *Cluster) Init(ctx context.Context) error {
for i, ind := range cr.storages {
if ind == -1 {
return fmt.Errorf("Storage %q does not exists", cr.opts.Storages[i])
}
}
return nil
}

Expand Down Expand Up @@ -172,7 +189,7 @@ func (cr *Cluster) enable(ctx context.Context) error {
Host: cr.gcfg.PublicHost,
Port: cr.gcfg.PublicPort,
Version: build.ClusterVersion,
Byoc: cr.gcfg.Byoc,
Byoc: cr.opts.Byoc,
NoFastEnable: cr.gcfg.NoFastEnable,
Flavor: ConfigFlavor{
Runtime: "golang/" + runtime.GOOS + "-" + runtime.GOARCH,
Expand Down
6 changes: 4 additions & 2 deletions cluster/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/LiterMC/go-openbmclapi/storage"
)

func (cr *Cluster) HandleFile(req *http.Request, rw http.ResponseWriter, hash string, size int64) {
func (cr *Cluster) HandleFile(rw http.ResponseWriter, req *http.Request, hash string) {
defer log.RecoverPanic(nil)

if !cr.Enabled() {
Expand Down Expand Up @@ -88,6 +88,8 @@ func (cr *Cluster) HandleFile(req *http.Request, rw http.ResponseWriter, hash st

api.SetAccessInfo(req, "cluster", cr.ID())

var size int64 = -1 // TODO: get the size

var (
sto storage.Storage
err error
Expand Down Expand Up @@ -121,7 +123,7 @@ func (cr *Cluster) HandleFile(req *http.Request, rw http.ResponseWriter, hash st
http.Error(rw, err.Error(), http.StatusInternalServerError)
}

func (cr *Cluster) HandleMeasure(req *http.Request, rw http.ResponseWriter, size int) {
func (cr *Cluster) HandleMeasure(rw http.ResponseWriter, req *http.Request, size int) {
if !cr.Enabled() {
// do not serve file if cluster is not enabled yet
http.Error(rw, "Cluster is not enabled yet", http.StatusServiceUnavailable)
Expand Down
2 changes: 1 addition & 1 deletion cluster/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (cr *Cluster) makeReqWithBody(
query url.Values, body io.Reader,
) (req *http.Request, err error) {
var u *url.URL
if u, err = url.Parse(cr.opts.Prefix); err != nil {
if u, err = url.Parse(cr.opts.Server); err != nil {
return
}
u.Path = path.Join(u.Path, relpath)
Expand Down
4 changes: 2 additions & 2 deletions cluster/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func (cr *Cluster) Connect(ctx context.Context) error {
}

engio, err := engine.NewSocket(engine.Options{
Host: cr.opts.Prefix,
Host: cr.opts.Server,
Path: "/socket.io/",
ExtraHeaders: http.Header{
"Origin": {cr.opts.Prefix},
"Origin": {cr.opts.Server},
"User-Agent": {build.ClusterUserAgent},
},
DialTimeout: time.Minute * 6,
Expand Down
40 changes: 24 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ package main

import (
"bytes"
"errors"
"fmt"
"net/url"
"os"

"gopkg.in/yaml.v3"

"github.com/LiterMC/go-openbmclapi/config"
"github.com/LiterMC/go-openbmclapi/log"
"github.com/LiterMC/go-openbmclapi/storage"
)

const DefaultBMCLAPIServer = "https://openbmclapi.bangbang93.com"

func migrateConfig(data []byte, cfg *config.Config) {
var oldConfig map[string]any
if err := yaml.Unmarshal(data, &oldConfig); err != nil {
Expand All @@ -45,11 +53,13 @@ func migrateConfig(data []byte, cfg *config.Config) {
if oldConfig["clusters"].(map[string]any) == nil {
id, ok1 := oldConfig["cluster-id"].(string)
secret, ok2 := oldConfig["cluster-secret"].(string)
if ok1 && ok2 {
cfg.Clusters = map[string]ClusterItem{
publicHost, ok3 := oldConfig["public-host"].(string)
if ok1 && ok2 && ok3 {
cfg.Clusters = map[string]config.ClusterOptions{
"main": {
Id: id,
Secret: secret,
Id: id,
Secret: secret,
PublicHosts: []string{publicHost},
},
}
}
Expand All @@ -67,7 +77,7 @@ func readAndRewriteConfig() (cfg *config.Config, err error) {
log.TrErrorf("error.config.read.failed", err)
os.Exit(1)
}
log.TrError("error.config.not.exists")
log.TrErrorf("error.config.not.exists")
notexists = true
} else {
migrateConfig(data, cfg)
Expand All @@ -76,15 +86,18 @@ func readAndRewriteConfig() (cfg *config.Config, err error) {
os.Exit(1)
}
if len(cfg.Clusters) == 0 {
cfg.Clusters = map[string]ClusterItem{
cfg.Clusters = map[string]config.ClusterOptions{
"main": {
Id: "${CLUSTER_ID}",
Secret: "${CLUSTER_SECRET}",
Id: "${CLUSTER_ID}",
Secret: "${CLUSTER_SECRET}",
PublicHosts: []string{},
Server: DefaultBMCLAPIServer,
SkipSignatureCheck: false,
},
}
}
if len(cfg.Certificates) == 0 {
cfg.Certificates = []CertificateConfig{
cfg.Certificates = []config.CertificateConfig{
{
Cert: "/path/to/cert.pem",
Key: "/path/to/key.pem",
Expand Down Expand Up @@ -123,12 +136,6 @@ func readAndRewriteConfig() (cfg *config.Config, err error) {
os.Exit(1)
}
ids[s.Id] = i
if s.Cluster != "" && s.Cluster != "-" {
if _, ok := cfg.Clusters[s.Cluster]; !ok {
log.Errorf("Storage %q is trying to connect to a not exists cluster %q.", s.Id, s.Cluster)
os.Exit(1)
}
}
}
}

Expand Down Expand Up @@ -173,7 +180,8 @@ func readAndRewriteConfig() (cfg *config.Config, err error) {
os.Exit(1)
}
if notexists {
log.TrError("error.config.created")
log.TrErrorf("error.config.created")
return nil, errors.New("Please edit the config before continue!")
}
return
}
4 changes: 1 addition & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Config struct {
PublicPort uint16 `yaml:"public-port"`
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
Byoc bool `yaml:"byoc"`
UseCert bool `yaml:"use-cert"`
TrustedXForwardedFor bool `yaml:"trusted-x-forwarded-for"`

Expand Down Expand Up @@ -65,7 +64,7 @@ type Config struct {
Advanced AdvancedConfig `yaml:"advanced"`
}

func (cfg *Config) applyWebManifest(manifest map[string]any) {
func (cfg *Config) ApplyWebManifest(manifest map[string]any) {
if cfg.Dashboard.Enable {
manifest["name"] = cfg.Dashboard.PwaName
manifest["short_name"] = cfg.Dashboard.PwaShortName
Expand All @@ -79,7 +78,6 @@ func NewDefaultConfig() *Config {
PublicPort: 0,
Host: "0.0.0.0",
Port: 4000,
Byoc: false,
TrustedXForwardedFor: false,

OnlyGcWhenStart: false,
Expand Down
17 changes: 15 additions & 2 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ import (
type ClusterOptions struct {
Id string `json:"id" yaml:"id"`
Secret string `json:"secret" yaml:"secret"`
Byoc bool `json:"byoc"`
PublicHosts []string `json:"public-hosts" yaml:"public-hosts"`
Prefix string `json:"prefix" yaml:"prefix"`
Server string `json:"server" yaml:"server"`
SkipSignatureCheck bool `json:"skip-signature-check" yaml:"skip-signature-check"`
Storages []string `json:"storages" yaml:"storages"`
}

type ClusterGeneralConfig struct {
PublicHost string `json:"public-host"`
PublicPort uint16 `json:"public-port"`
Byoc bool `json:"byoc"`
NoFastEnable bool `json:"no-fast-enable"`
MaxReconnectCount int `json:"max-reconnect-count"`
}
Expand Down Expand Up @@ -77,6 +78,10 @@ type CacheConfig struct {
newCache func() cache.Cache `yaml:"-"`
}

func (c *CacheConfig) NewCache() cache.Cache {
return c.newCache()
}

func (c *CacheConfig) UnmarshalYAML(n *yaml.Node) (err error) {
var cfg struct {
Type string `yaml:"type"`
Expand Down Expand Up @@ -148,3 +153,11 @@ func (c *TunnelConfig) UnmarshalYAML(n *yaml.Node) (err error) {
}
return
}

func (c *TunnelConfig) MatchTunnelOutput(line []byte) (host, port []byte, ok bool) {
res := c.outputRegex.FindSubmatch(line)
if res == nil {
return
}
return res[c.hostOut], res[c.portOut], true
}
3 changes: 2 additions & 1 deletion dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ var dsbManifest = func() (dsbManifest map[string]any) {
return
}()

func (r *Runner) serveDashboard(rw http.ResponseWriter, req *http.Request, pth string) {
func (r *Runner) serveDashboard(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet && req.Method != http.MethodHead {
rw.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
http.Error(rw, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return
}
acceptEncoding := utils.SplitCSV(req.Header.Get("Accept-Encoding"))
pth := strings.TrimPrefix(req.URL.Path, "/")
switch pth {
case "":
break
Expand Down
Loading

0 comments on commit 173aced

Please sign in to comment.