123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package redirection
- import (
- "encoding/json"
- "fmt"
- "log"
- "os"
- "path"
- "path/filepath"
- "regexp"
- "strings"
- "sync"
- "imuslab.com/zoraxy/mod/info/logger"
- "imuslab.com/zoraxy/mod/utils"
- )
- type RuleTable struct {
- AllowRegex bool
- Logger *logger.Logger
- configPath string
- rules sync.Map
- }
- type RedirectRules struct {
- RedirectURL string
- TargetURL string
- ForwardChildpath bool
- StatusCode int
- }
- func NewRuleTable(configPath string, allowRegex bool, logger *logger.Logger) (*RuleTable, error) {
- thisRuleTable := RuleTable{
- rules: sync.Map{},
- configPath: configPath,
- AllowRegex: allowRegex,
- Logger: logger,
- }
-
- if !utils.FileExists(configPath) {
- os.MkdirAll(configPath, 0775)
- }
-
- files, err := filepath.Glob(filepath.Join(configPath, "*.json"))
- if err != nil {
- return nil, err
- }
-
- var rules []*RedirectRules
- for _, file := range files {
- b, err := os.ReadFile(file)
- if err != nil {
- continue
- }
- thisRule := RedirectRules{}
- err = json.Unmarshal(b, &thisRule)
- if err != nil {
- continue
- }
- rules = append(rules, &thisRule)
- }
-
- for _, rule := range rules {
- thisRuleTable.log("Redirection rule added: "+rule.RedirectURL+" -> "+rule.TargetURL, nil)
- thisRuleTable.rules.Store(rule.RedirectURL, rule)
- }
- return &thisRuleTable, nil
- }
- func (t *RuleTable) AddRedirectRule(redirectURL string, destURL string, forwardPathname bool, statusCode int) error {
-
- newRule := &RedirectRules{
- RedirectURL: redirectURL,
- TargetURL: destURL,
- ForwardChildpath: forwardPathname,
- StatusCode: statusCode,
- }
-
- filename := utils.ReplaceSpecialCharacters(redirectURL) + ".json"
-
- filepath := path.Join(t.configPath, filename)
-
- file, err := os.Create(filepath)
- if err != nil {
- t.log("Error creating file "+filepath, err)
- return err
- }
- defer file.Close()
-
- err = json.NewEncoder(file).Encode(newRule)
- if err != nil {
- t.log("Error encoding JSON to file "+filepath, err)
- return err
- }
-
- t.rules.Store(redirectURL, newRule)
- return nil
- }
- func (t *RuleTable) DeleteRedirectRule(redirectURL string) error {
-
- filename := utils.ReplaceSpecialCharacters(redirectURL) + ".json"
-
- filepath := path.Join(t.configPath, filename)
- fmt.Println(redirectURL, filename, filepath)
-
- if _, err := os.Stat(filepath); os.IsNotExist(err) {
- return nil
- }
-
- if err := os.Remove(filepath); err != nil {
- t.log("Error deleting file "+filepath, err)
- return err
- }
-
- t.rules.Delete(redirectURL)
- return nil
- }
- func (t *RuleTable) GetAllRedirectRules() []*RedirectRules {
- rules := []*RedirectRules{}
- t.rules.Range(func(key, value interface{}) bool {
- r, ok := value.(*RedirectRules)
- if ok {
- rules = append(rules, r)
- }
- return true
- })
- return rules
- }
- func (t *RuleTable) MatchRedirectRule(requestedURL string) *RedirectRules {
-
- var targetRedirectionRule *RedirectRules = nil
- var maxMatch int = 0
- t.rules.Range(func(key interface{}, value interface{}) bool {
-
- if t.AllowRegex {
-
- matched, err := regexp.MatchString(key.(string), requestedURL)
- if err != nil {
-
- t.log("Unable to match regex", err)
- return true
- }
- if matched {
- maxMatch = len(key.(string))
- targetRedirectionRule = value.(*RedirectRules)
- }
- } else {
-
- if strings.HasPrefix(requestedURL, key.(string)) {
-
- if len(key.(string)) > maxMatch {
- maxMatch = len(key.(string))
- targetRedirectionRule = value.(*RedirectRules)
- }
- }
- }
- return true
- })
- return targetRedirectionRule
- }
- func (t *RuleTable) log(message string, err error) {
- if t.Logger == nil {
- if err == nil {
- log.Println("[Redirect] " + message)
- } else {
- log.Println("[Redirect] " + message + ": " + err.Error())
- }
- } else {
- t.Logger.PrintAndLog("Redirect", message, err)
- }
- }
|