Skip to content

Commit

Permalink
Chore: remove forward compatible code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jan 1, 2022
1 parent 7267c58 commit 204a72b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 67 deletions.
11 changes: 0 additions & 11 deletions adapter/outbound/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ type VmessOption struct {
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
WSOpts WSOptions `proxy:"ws-opts,omitempty"`

// TODO: remove these until 2022
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
WSPath string `proxy:"ws-path,omitempty"`
}

type HTTPOptions struct {
Expand Down Expand Up @@ -80,13 +76,6 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
var err error
switch v.option.Network {
case "ws":
if v.option.WSOpts.Path == "" {
v.option.WSOpts.Path = v.option.WSPath
}
if len(v.option.WSOpts.Headers) == 0 {
v.option.WSOpts.Headers = v.option.WSHeaders
}

host, port, _ := net.SplitHostPort(v.addr)
wsOpts := &vmess.WebsocketConfig{
Host: host,
Expand Down
71 changes: 15 additions & 56 deletions component/profile/cachefile/cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cachefile

import (
"bytes"
"encoding/gob"
"os"
"sync"
"time"
Expand Down Expand Up @@ -138,69 +136,30 @@ func (c *CacheFile) Close() error {
return c.DB.Close()
}

// TODO: remove migrateCache until 2022
func migrateCache() {
defer func() {
options := bbolt.Options{Timeout: time.Second}
db, err := bbolt.Open(C.Path.Cache(), fileMode, &options)
switch err {
case bbolt.ErrInvalid, bbolt.ErrChecksum, bbolt.ErrVersionMismatch:
if err = os.Remove(C.Path.Cache()); err != nil {
log.Warnln("[CacheFile] remove invalid cache file error: %s", err.Error())
break
}
log.Infoln("[CacheFile] remove invalid cache file and create new one")
db, err = bbolt.Open(C.Path.Cache(), fileMode, &options)
}
if err != nil {
log.Warnln("[CacheFile] can't open cache file: %s", err.Error())
func initCache() {
options := bbolt.Options{Timeout: time.Second}
db, err := bbolt.Open(C.Path.Cache(), fileMode, &options)
switch err {
case bbolt.ErrInvalid, bbolt.ErrChecksum, bbolt.ErrVersionMismatch:
if err = os.Remove(C.Path.Cache()); err != nil {
log.Warnln("[CacheFile] remove invalid cache file error: %s", err.Error())
break
}

defaultCache = &CacheFile{
DB: db,
}
}()

buf, err := os.ReadFile(C.Path.OldCache())
if err != nil {
return
}
defer os.Remove(C.Path.OldCache())

// read old cache file
type cache struct {
Selected map[string]string
}
model := &cache{
Selected: map[string]string{},
log.Infoln("[CacheFile] remove invalid cache file and create new one")
db, err = bbolt.Open(C.Path.Cache(), fileMode, &options)
}
bufReader := bytes.NewBuffer(buf)
gob.NewDecoder(bufReader).Decode(model)

// write to new cache file
db, err := bbolt.Open(C.Path.Cache(), fileMode, nil)
if err != nil {
return
log.Warnln("[CacheFile] can't open cache file: %s", err.Error())
}
defer db.Close()

db.Batch(func(t *bbolt.Tx) error {
bucket, err := t.CreateBucketIfNotExists(bucketSelected)
if err != nil {
return err
}
for group, selected := range model.Selected {
if err := bucket.Put([]byte(group), []byte(selected)); err != nil {
return err
}
}
return nil
})
defaultCache = &CacheFile{
DB: db,
}
}

// Cache return singleton of CacheFile
func Cache() *CacheFile {
initOnce.Do(migrateCache)
initOnce.Do(initCache)

return defaultCache
}

0 comments on commit 204a72b

Please sign in to comment.