123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package redirection
- import (
- "encoding/json"
- "log"
- "os"
- "path"
- "path/filepath"
- "strings"
- "sync"
- "imuslab.com/arozos/ReverseProxy/mod/utils"
- )
- type RuleTable struct {
- configPath string
- rules sync.Map
- }
- type RedirectRules struct {
- RedirectURL string
- TargetURL string
- ForwardChildpath bool
- StatusCode int
- }
- func NewRuleTable(configPath string) (*RuleTable, error) {
- thisRuleTable := RuleTable{
- rules: sync.Map{},
- configPath: configPath,
- }
-
- 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 {
- log.Println("Redirection rule added: " + rule.RedirectURL + " -> " + rule.TargetURL)
- 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 := strings.ReplaceAll(strings.ReplaceAll(redirectURL, "/", "-"), ".", "_") + ".json"
-
- filepath := path.Join(t.configPath, filename)
-
- file, err := os.Create(filepath)
- if err != nil {
- log.Printf("Error creating file %s: %s", filepath, err)
- return err
- }
- defer file.Close()
-
- err = json.NewEncoder(file).Encode(newRule)
- if err != nil {
- log.Printf("Error encoding JSON to file %s: %s", filepath, err)
- return err
- }
-
- t.rules.Store(redirectURL, newRule)
- return nil
- }
- func (t *RuleTable) DeleteRedirectRule(redirectURL string) error {
-
- filename := strings.ReplaceAll(strings.ReplaceAll(redirectURL, "/", "-"), ".", "_") + ".json"
-
- filepath := path.Join(t.configPath, filename)
-
- if _, err := os.Stat(filepath); os.IsNotExist(err) {
- return nil
- }
-
- if err := os.Remove(filepath); err != nil {
- log.Printf("Error deleting file %s: %s", 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 strings.HasPrefix(requestedURL, key.(string)) {
-
- if len(key.(string)) > maxMatch {
- maxMatch = len(key.(string))
- targetRedirectionRule = value.(*RedirectRules)
- }
- }
- return true
- })
- return targetRedirectionRule
- }
|