├── .gitignore ├── LICENSE.md ├── README.md ├── cmd └── main.go ├── go.mod ├── go.sum ├── internal ├── monitor │ ├── monitor.go │ ├── type.go │ └── types.go ├── monitor_manager │ └── monitor_manager.go ├── profile │ ├── profile.go │ ├── profile_group.go │ └── types.go ├── proxy │ ├── proxy.go │ ├── proxy_group.go │ └── types.go ├── task │ ├── task.go │ ├── task_group.go │ ├── type.go │ └── types.go ├── task_manager │ └── task_manager.go └── utils │ └── address.go ├── monitors ├── dummy │ └── dummy_monitor.go └── footsites │ ├── footsites.go │ ├── initialize.go │ ├── states.go │ ├── stock.go │ └── types.go ├── sites ├── dummy_site │ └── dummy_site.go └── footsites │ ├── billing.go │ ├── cart.go │ ├── email.go │ ├── footsites.go │ ├── initialize.go │ ├── monitor.go │ ├── order.go │ ├── session.go │ ├── shipping.go │ ├── states.go │ ├── types.go │ └── util.go └── third_party └── hclient ├── client.go ├── request.go ├── response.go └── types.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Remove IDE configuration 9 | .idea/ 10 | 11 | # Test binary, built with `go test -c` 12 | *.test 13 | 14 | # Output of the go coverage tool, specifically when used with LiteIDE 15 | *.out 16 | 17 | # Dependency directories (remove the comment below to include it) 18 | # vendor/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 EdwinJ0124 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **DEPRACATED** 2 | 3 | # Bot-Base 4 | 5 | Bot-Base is a small project with concepts for most elements of a bot. 6 | 7 | Feel free to contact me on [Twitter](https://twitter.com/pristine1862) with any questions. 8 | 9 | ## Contributing 10 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 11 | 12 | Please make sure to update tests as appropriate. 13 | 14 | ## Todo 15 | - Add tests 16 | - Merge monitoring and regular tasks to reduce code redundancy 17 | - Better monitor notifications 18 | 19 | ## License 20 | [MIT](https://github.com/EdwinJ0124/footsites/blob/master/LICENSE.md) 21 | -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/EdwinJ0124/bot-base/monitors/footsites" 5 | "github.com/EdwinJ0124/bot-base/sites/footsites" 6 | ) 7 | 8 | func main() { 9 | footsitesmntr.Initialize() 10 | footsites.Initialize() 11 | } 12 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/EdwinJ0124/bot-base 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/google/uuid v1.2.0 // indirect 7 | github.com/iancoleman/orderedmap v0.2.0 8 | github.com/lithammer/shortuuid v3.0.0+incompatible 9 | ) 10 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= 2 | github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 3 | github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA= 4 | github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= 5 | github.com/lithammer/shortuuid v3.0.0+incompatible h1:NcD0xWW/MZYXEHa6ITy6kaXN5nwm/V115vj2YXfhS0w= 6 | github.com/lithammer/shortuuid v3.0.0+incompatible/go.mod h1:FR74pbAuElzOUuenUHTK2Tciko1/vKuIKS9dSkDrA4w= 7 | -------------------------------------------------------------------------------- /internal/monitor/monitor.go: -------------------------------------------------------------------------------- 1 | package monitor 2 | 3 | import ( 4 | "errors" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | "github.com/lithammer/shortuuid" 7 | "sync" 8 | ) 9 | 10 | var ( 11 | monitorMutex = sync.RWMutex{} 12 | 13 | MonitorNotInTaskGroupErr = errors.New("monitor not in any task group") 14 | MonitorDoesNotExistErr = errors.New("monitor does not exist") 15 | 16 | monitors = make(map[string]*Monitor) 17 | ) 18 | 19 | // DoesMonitorExist checks if a monitor exists 20 | func DoesMonitorExist(id string) bool { 21 | monitorMutex.RLock() 22 | defer monitorMutex.RUnlock() 23 | _, ok := monitors[id] 24 | return ok 25 | } 26 | 27 | // CreateMonitor creates a monitor 28 | func CreateMonitor(monitorType, site, product string) string { 29 | monitorMutex.Lock() 30 | defer monitorMutex.Unlock() 31 | id := shortuuid.New() 32 | 33 | monitors[id] = &Monitor{ 34 | ID: id, 35 | Product: product, 36 | Type: monitorType, 37 | Site: site, 38 | } 39 | 40 | return id 41 | } 42 | 43 | // RemoveMonitor removes a monitor 44 | func RemoveMonitor(id string) error { 45 | if !DoesMonitorExist(id) { 46 | return MonitorDoesNotExistErr 47 | } 48 | 49 | monitorMutex.Lock() 50 | defer monitorMutex.Unlock() 51 | 52 | monitor := monitors[id] 53 | monitor.Cancel() 54 | 55 | delete(monitors, id) 56 | 57 | return nil 58 | } 59 | 60 | // GetMonitor gets a monitor 61 | func GetMonitor(id string) (*Monitor, error) { 62 | if !DoesMonitorExist(id) { 63 | return &Monitor{}, MonitorDoesNotExistErr 64 | } 65 | 66 | monitorMutex.RLock() 67 | defer monitorMutex.RUnlock() 68 | 69 | return monitors[id], nil 70 | } 71 | 72 | // SetMonitorToTaskGroup sets a task group to a monitor 73 | func SetMonitorToTaskGroup(monitorId, taskGroupId string) error { 74 | if !DoesMonitorExist(monitorId) { 75 | return MonitorDoesNotExistErr 76 | } 77 | 78 | if !task.DoesTaskGroupExist(taskGroupId) { 79 | return task.TaskGroupDoesNotExistErr 80 | } 81 | 82 | taskGroup, _ := task.GetTaskGroup(monitorId) 83 | taskGroup.Monitors[monitorId] = true 84 | 85 | return nil 86 | } 87 | 88 | // NotifyTasks notifies tasks 89 | func (m *Monitor) NotifyTasks(monitorData interface{}) error { 90 | associatedTaskGroup, err := m.getAssociatedTaskGroup() 91 | 92 | if err != nil { 93 | return err 94 | } 95 | 96 | taskIds := associatedTaskGroup.GetAllTaskIDs() 97 | 98 | for _, id := range taskIds { 99 | task, _ := task.GetTask(id) 100 | 101 | task.MonitorData <- monitorData 102 | } 103 | 104 | return nil 105 | } 106 | 107 | func (m *Monitor) getAssociatedTaskGroup() (*task.TaskGroup, error) { 108 | taskGroupIds := task.GetAllTaskGroupIDs() 109 | 110 | for _, taskGroupId := range taskGroupIds { 111 | taskGroup, _ := task.GetTaskGroup(taskGroupId) 112 | 113 | if taskGroup.Monitors[m.ID] { 114 | return taskGroup, nil 115 | } 116 | } 117 | 118 | return &task.TaskGroup{}, MonitorNotInTaskGroupErr 119 | } 120 | -------------------------------------------------------------------------------- /internal/monitor/type.go: -------------------------------------------------------------------------------- 1 | package monitor 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | ) 7 | 8 | var ( 9 | DoneMonitorState MonitorState = "done" 10 | ErrorMonitorState MonitorState = "error" 11 | 12 | MonitorTypeDoesNotExistErr = errors.New("monitor type does not exist") 13 | MonitorHandlerDoesNotExistErr = errors.New("monitor handler does not exist") 14 | 15 | monitorTypes = make(map[string]*MonitorType) 16 | ) 17 | 18 | // RegisterMonitorType register monitor type 19 | func RegisterMonitorType(monitorType string) *MonitorType { 20 | monitorTypes[monitorType] = &MonitorType{ 21 | handlers: make(MonitorReflectMap), 22 | } 23 | 24 | return monitorTypes[monitorType] 25 | } 26 | 27 | // DoesMonitorTypeExist check if monitor type exists 28 | func DoesMonitorTypeExist(monitorType string) bool { 29 | _, ok := monitorTypes[monitorType] 30 | return ok 31 | } 32 | 33 | // GetMonitorType gets a task type 34 | func GetMonitorType(monitorType string) (*MonitorType, error) { 35 | if !DoesMonitorTypeExist(monitorType) { 36 | return &MonitorType{}, MonitorTypeDoesNotExistErr 37 | } 38 | return monitorTypes[monitorType], nil 39 | } 40 | 41 | // HasHandlers check if there are handlers 42 | func (t *MonitorType) HasHandlers() bool { 43 | return len(t.handlers) > 0 44 | } 45 | 46 | func (t *MonitorType) addHandler(handlerName MonitorState, handler interface{}) { 47 | t.handlers[string(handlerName)] = reflect.ValueOf(handler) 48 | } 49 | 50 | // AddHandlers adds multiple handles to a monitor type 51 | func (t *MonitorType) AddHandlers(handlers MonitorHandlerMap) { 52 | for handlerName, handler := range handlers { 53 | if t.internalType == nil { 54 | handleTypes := reflect.TypeOf(handler) 55 | // func (t *task.Task, internal *SiteInternal) task.TaskState 56 | 57 | // we want the second one because the first one (0 index) will be task.Task type 58 | handleType := handleTypes.In(1) 59 | 60 | t.internalType = handleType 61 | } 62 | 63 | t.addHandler(handlerName, handler) 64 | } 65 | } 66 | 67 | // GetHandler gets a handler by handler name 68 | func (t *MonitorType) GetHandler(handlerName MonitorState) (reflect.Value, error) { 69 | handler, ok := t.handlers[string(handlerName)] 70 | 71 | if !ok { 72 | return reflect.Value{}, MonitorHandlerDoesNotExistErr 73 | } 74 | 75 | return handler, nil 76 | } 77 | 78 | // GetFirstHandlerState gets the first handler state 79 | func (t *MonitorType) GetFirstHandlerState() MonitorState { 80 | return t.firstHandlerState 81 | } 82 | 83 | // SetFirstHandlerState sets the first handler state 84 | func (t *MonitorType) SetFirstHandlerState(firstHandlerState MonitorState) { 85 | t.firstHandlerState = firstHandlerState 86 | } 87 | 88 | // GetInternalType gets the internal type 89 | func (t *MonitorType) GetInternalType() reflect.Type { 90 | return t.internalType 91 | } 92 | -------------------------------------------------------------------------------- /internal/monitor/types.go: -------------------------------------------------------------------------------- 1 | package monitor 2 | 3 | import ( 4 | "context" 5 | "github.com/EdwinJ0124/bot-base/third_party/hclient" 6 | "reflect" 7 | ) 8 | 9 | type Monitor struct { 10 | ID string `json:"id"` 11 | Site string `json:"site"` 12 | Type string `json:"type"` 13 | Product string `json:"input"` 14 | ProxyListID string `json:"proxyListID"` 15 | Internal interface{} `json:"-"` 16 | Context context.Context `json:"-"` 17 | Cancel context.CancelFunc `json:"-"` 18 | Client *hclient.Client `json:"-"` 19 | Active bool `json:"-"` 20 | } 21 | 22 | type MonitorType struct { 23 | firstHandlerState MonitorState 24 | internalType reflect.Type 25 | handlers MonitorReflectMap 26 | } 27 | 28 | type MonitorState string 29 | type MonitorHandlerMap map[MonitorState]interface{} 30 | type MonitorReflectMap map[string]reflect.Value 31 | -------------------------------------------------------------------------------- /internal/monitor_manager/monitor_manager.go: -------------------------------------------------------------------------------- 1 | package monitormngr 2 | 3 | import ( 4 | "context" 5 | "github.com/EdwinJ0124/bot-base/internal/monitor" 6 | "reflect" 7 | "time" 8 | ) 9 | 10 | func handleMonitorState(monitorState monitor.MonitorState, monitorType *monitor.MonitorType, t *monitor.Monitor) monitor.MonitorState { 11 | nextHandler, err := monitorType.GetHandler(monitorState) 12 | 13 | if err != nil { 14 | return monitor.ErrorMonitorState 15 | } 16 | 17 | // func (m *monitor.Monitor, internal *MonitorInternal) monitor.MonitorState 18 | nextNextTaskType := nextHandler.Call([]reflect.Value{reflect.ValueOf(t), reflect.ValueOf(t.Internal)}) 19 | 20 | // monitor.MonitorState 21 | return monitor.MonitorState(nextNextTaskType[0].String()) 22 | } 23 | 24 | // RunMonitor starts a monitor task 25 | func RunMonitor(m *monitor.Monitor) { 26 | m.Context, m.Cancel = context.WithCancel(context.Background()) 27 | m.Active = true 28 | 29 | defer func() { 30 | if r := recover(); r != nil { 31 | // handle crash 32 | } 33 | }() 34 | 35 | if !monitor.DoesMonitorTypeExist(m.Type) { 36 | return 37 | } 38 | 39 | monitorType, err := monitor.GetMonitorType(m.Type) 40 | 41 | if err != nil { 42 | m.Active = false 43 | return 44 | } 45 | 46 | hasHandlers := monitorType.HasHandlers() 47 | 48 | if !hasHandlers { 49 | m.Active = false 50 | return 51 | } 52 | 53 | nextState := monitorType.GetFirstHandlerState() 54 | 55 | if len(nextState) == 0 { 56 | m.Active = false 57 | return 58 | } 59 | 60 | m.Internal = reflect.New(monitorType.GetInternalType().Elem()).Interface() 61 | 62 | // loop the moniitor states 63 | for { 64 | nextState = handleMonitorState(nextState, monitorType, m) 65 | 66 | if nextState == monitor.DoneMonitorState || m.Context.Err() != nil { 67 | // you can report that the monitor stopped here 68 | m.Active = false 69 | break 70 | } else if nextState == monitor.ErrorMonitorState { 71 | // report errors 72 | m.Active = false 73 | break 74 | } 75 | 76 | time.Sleep(1 * time.Millisecond) 77 | } 78 | } 79 | 80 | // StopMonitor stops a monitor task 81 | func StopMonitor(m *monitor.Monitor) { 82 | m.Cancel() 83 | } 84 | -------------------------------------------------------------------------------- /internal/profile/profile.go: -------------------------------------------------------------------------------- 1 | package profile 2 | 3 | import ( 4 | "errors" 5 | "github.com/lithammer/shortuuid" 6 | ) 7 | 8 | var ( 9 | ProfileDoesNotExistErr = errors.New("profile does not exist") 10 | ProfileNotAssignedErr = errors.New("profile not assigned") 11 | profiles = make(map[string]*Profile) 12 | ) 13 | 14 | // DoesProfileExist checks if a profile exists 15 | func DoesProfileExist(id string) bool { 16 | _, ok := profiles[id] 17 | return ok 18 | } 19 | 20 | // CreateProfile creates a new profile 21 | func CreateProfile(profile *Profile) string { 22 | id := shortuuid.New() 23 | 24 | profiles[id] = profile 25 | 26 | return id 27 | } 28 | 29 | // RemoveProfile removes a profile 30 | func RemoveProfile(id string) error { 31 | if !DoesProfileExist(id) { 32 | return ProfileGroupDoesNotExistErr 33 | } 34 | 35 | delete(profiles, id) 36 | 37 | return nil 38 | } 39 | 40 | // GetProfileById gets a profile by id 41 | func GetProfileById(id string) (*Profile, error) { 42 | if !DoesProfileExist(id) { 43 | return &Profile{}, ProfileGroupDoesNotExistErr 44 | } 45 | 46 | return profiles[id], nil 47 | } 48 | 49 | // GetAllProfileIDs gets all profile ids 50 | func GetAllProfileIDs() []string { 51 | ids := []string{} 52 | 53 | for id := range profiles { 54 | ids = append(ids, id) 55 | } 56 | 57 | return ids 58 | } 59 | 60 | // SetProfileToProfileGroup sets a profile to a profile group 61 | func SetProfileToProfileGroup(profileId, profileGroupId string) error { 62 | if !DoesProfileExist(profileId) { 63 | return ProfileDoesNotExistErr 64 | } 65 | 66 | if !DoesProfileGroupExist(profileGroupId) { 67 | return ProfileGroupDoesNotExistErr 68 | } 69 | 70 | profileGroups[profileGroupId].Profiles.Set(profileId, true) 71 | 72 | return nil 73 | } 74 | 75 | // RemoveProfileFromProfileGroup removes a profile from a profile group 76 | func RemoveProfileFromProfileGroup(profileId, profileGroupId string) error { 77 | if !DoesProfileExist(profileId) { 78 | return ProfileDoesNotExistErr 79 | } 80 | 81 | if !DoesProfileGroupExist(profileGroupId) { 82 | return ProfileGroupDoesNotExistErr 83 | } 84 | 85 | profileGroups[profileGroupId].Profiles.Delete(profileId) 86 | 87 | return nil 88 | } 89 | -------------------------------------------------------------------------------- /internal/profile/profile_group.go: -------------------------------------------------------------------------------- 1 | package profile 2 | 3 | import ( 4 | "errors" 5 | "github.com/iancoleman/orderedmap" 6 | "github.com/lithammer/shortuuid" 7 | ) 8 | 9 | var ( 10 | ProfileGroupEmptyErr = errors.New("profile group does not contain any profiles") 11 | ProfileGroupDoesNotExistErr = errors.New("profile group does not exist") 12 | profileGroups = make(map[string]*ProfileGroup) 13 | ) 14 | 15 | // DoesProfileGroupExist checks if a profile group exists 16 | func DoesProfileGroupExist(id string) bool { 17 | _, ok := profileGroups[id] 18 | return ok 19 | } 20 | 21 | // CreateProfileGroup creates a new profile group 22 | func CreateProfileGroup(name string) string { 23 | id := shortuuid.New() 24 | 25 | profileGroups[id] = &ProfileGroup{ 26 | Name: name, 27 | ID: id, 28 | Profiles: orderedmap.New(), 29 | } 30 | 31 | return id 32 | } 33 | 34 | // RemoveProfileGroup removes a profile group 35 | func RemoveProfileGroup(id string) error { 36 | if !DoesProfileGroupExist(id) { 37 | return ProfileGroupDoesNotExistErr 38 | } 39 | 40 | delete(profileGroups, id) 41 | 42 | return nil 43 | } 44 | 45 | // GetProfileGroup gets a profile group by id 46 | func GetProfileGroup(id string) (*ProfileGroup, error) { 47 | if !DoesProfileGroupExist(id) { 48 | return &ProfileGroup{}, ProfileGroupDoesNotExistErr 49 | } 50 | 51 | return profileGroups[id], nil 52 | } 53 | 54 | // GetProfileFromProfileGroup gets a profile from a profile group 55 | // Gets the first profile, then moves it to the back 56 | func GetProfileFromProfileGroup(id string) (*Profile, error) { 57 | if !DoesProfileGroupExist(id) { 58 | return &Profile{}, ProfileGroupDoesNotExistErr 59 | } 60 | 61 | profileGroup := profileGroups[id] 62 | 63 | profileIds := profileGroup.Profiles.Keys() 64 | 65 | if len(profileIds) == 0 { 66 | return &Profile{}, ProfileGroupEmptyErr 67 | } 68 | 69 | firstProfileId := profileIds[0] 70 | 71 | profile, err := GetProfileById(firstProfileId) 72 | 73 | if err != nil { 74 | return &Profile{}, err 75 | } 76 | 77 | profileGroup.Profiles.Delete(firstProfileId) 78 | 79 | profileGroup.Profiles.Set(firstProfileId, true) 80 | 81 | return profile, nil 82 | } 83 | 84 | // GetAllProfileGroupIds gets all profile group ids 85 | func GetAllProfileGroupIds() []string { 86 | ids := make([]string, 0) 87 | 88 | for id := range profileGroups { 89 | ids = append(ids, id) 90 | } 91 | 92 | return ids 93 | } 94 | -------------------------------------------------------------------------------- /internal/profile/types.go: -------------------------------------------------------------------------------- 1 | package profile 2 | 3 | import "github.com/iancoleman/orderedmap" 4 | 5 | type Profile struct { 6 | Name string `json:"name"` 7 | ProfileGroup string `json:"profileGroup"` 8 | BillingAddress struct { 9 | Name string `json:"name"` 10 | Email string `json:"email"` 11 | Phone string `json:"phone"` 12 | Line1 string `json:"line1"` 13 | Line2 string `json:"line2"` 14 | Line3 string `json:"line3"` 15 | Postcode string `json:"postCode"` 16 | City string `json:"city"` 17 | Country string `json:"country"` 18 | State string `json:"state"` 19 | } `json:"billingAddress"` 20 | ShippingAddress struct { 21 | Name string `json:"name"` 22 | Email string `json:"email"` 23 | Phone string `json:"phone"` 24 | Line1 string `json:"line1"` 25 | Line2 string `json:"line2"` 26 | Line3 string `json:"line3"` 27 | Postcode string `json:"postCode"` 28 | City string `json:"city"` 29 | Country string `json:"country"` 30 | State string `json:"state"` 31 | } `json:"shippingAddress"` 32 | PaymentDetails struct { 33 | NameOnCard string `json:"nameOnCard"` 34 | CardType string `json:"cardType"` 35 | CardNumber string `json:"cardNumber"` 36 | CardExpMonth string `json:"cardExpMonth"` 37 | CardExpYear string `json:"cardExpYear"` 38 | CardCvv string `json:"cardCvv"` 39 | } `json:"paymentDetails"` 40 | SameBillingAndShipping bool `json:"sameBillingAndShippingAddress"` 41 | } 42 | 43 | type ProfileGroup struct { 44 | ID string `json:"id"` 45 | Name string `json:"name"` 46 | Profiles *orderedmap.OrderedMap `json:"profiles"` // ordered map to make sure our profile selection works 47 | } 48 | -------------------------------------------------------------------------------- /internal/proxy/proxy.go: -------------------------------------------------------------------------------- 1 | package proxy 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "github.com/lithammer/shortuuid" 7 | "strings" 8 | "sync" 9 | ) 10 | 11 | var ( 12 | proxyMutex = sync.RWMutex{} 13 | 14 | ProxyDoesNotExistErr = errors.New("proxy does not exist") 15 | proxies = make(map[string]*Proxy) 16 | ) 17 | 18 | // DoesProxyExist checks if a proxy exists 19 | func DoesProxyExist(id string) bool { 20 | proxyMutex.RLock() 21 | defer proxyMutex.RUnlock() 22 | _, ok := proxies[id] 23 | return ok 24 | } 25 | 26 | func proxyToProxyUrl(proxy string) string { 27 | proxySplit := strings.Split(proxy, ":") 28 | 29 | if len(proxySplit) == 2 { 30 | return fmt.Sprintf("http://%s:%s", proxySplit[0], proxySplit[1]) 31 | } else if len(proxySplit) == 4 { 32 | return fmt.Sprintf("http://%s:%s@%s:%s", proxySplit[2], proxySplit[3], proxySplit[0], proxySplit[1]) 33 | } 34 | 35 | return fmt.Sprintf("http://%s", proxy) 36 | } 37 | 38 | // CreateProxy creates a proxy 39 | func CreateProxy(proxy string) string { 40 | proxyMutex.Lock() 41 | defer proxyMutex.Unlock() 42 | 43 | id := shortuuid.New() 44 | 45 | proxies[id] = &Proxy{ 46 | URL: proxyToProxyUrl(proxy), 47 | } 48 | 49 | return id 50 | } 51 | 52 | // RemoveProxy removes a proxy 53 | func RemoveProxy(id string) error { 54 | if !DoesProxyExist(id) { 55 | return ProxyDoesNotExistErr 56 | } 57 | 58 | proxyMutex.Lock() 59 | defer proxyMutex.Unlock() 60 | 61 | delete(proxies, id) 62 | return nil 63 | } 64 | 65 | func GetProxy(id string) (*Proxy, error) { 66 | if !DoesProxyExist(id) { 67 | return &Proxy{}, ProxyDoesNotExistErr 68 | } 69 | 70 | proxyMutex.RLock() 71 | defer proxyMutex.RUnlock() 72 | 73 | return proxies[id], nil 74 | } 75 | 76 | // SetProxyToProxyGroup sets a proxy to a proxy group 77 | func SetProxyToProxyGroup(proxyId, proxyGroupId string) error { 78 | if !DoesProxyExist(proxyId) { 79 | return ProxyDoesNotExistErr 80 | } 81 | 82 | if !DoesProxyGroupExist(proxyGroupId) { 83 | return ProxyGroupDoesNotExistErr 84 | } 85 | 86 | proxyGroup := proxyGroups[proxyGroupId] 87 | 88 | proxyGroup.Proxies.Set(proxyId, true) 89 | 90 | return nil 91 | } 92 | -------------------------------------------------------------------------------- /internal/proxy/proxy_group.go: -------------------------------------------------------------------------------- 1 | package proxy 2 | 3 | import ( 4 | "errors" 5 | "github.com/iancoleman/orderedmap" 6 | "github.com/lithammer/shortuuid" 7 | "sync" 8 | ) 9 | 10 | var ( 11 | proxyGroupMutex = sync.RWMutex{} 12 | 13 | ProxyGroupEmptyErr = errors.New("proxy group does not contain any proxies") 14 | ProxyGroupDoesNotExistErr = errors.New("proxy group does not exist") 15 | proxyGroups = make(map[string]*ProxyGroup) 16 | ) 17 | 18 | // DoesProxyGroupExist checks if a proxy group exists 19 | func DoesProxyGroupExist(id string) bool { 20 | proxyGroupMutex.RLock() 21 | defer proxyGroupMutex.RUnlock() 22 | _, ok := proxyGroups[id] 23 | return ok 24 | } 25 | 26 | // CreateProxyGroup creates a new proxy group 27 | func CreateProxyGroup(name string) string { 28 | proxyGroupMutex.Lock() 29 | defer proxyGroupMutex.Unlock() 30 | id := shortuuid.New() 31 | 32 | proxyGroups[id] = &ProxyGroup{ 33 | ID: id, 34 | Name: name, 35 | Proxies: orderedmap.New(), 36 | } 37 | 38 | return id 39 | } 40 | 41 | // RemoveProxyGroup removes a proxy group 42 | func RemoveProxyGroup(id string) error { 43 | if !DoesProxyGroupExist(id) { 44 | return ProxyGroupDoesNotExistErr 45 | } 46 | 47 | proxyGroupMutex.Lock() 48 | defer proxyGroupMutex.Unlock() 49 | 50 | delete(proxyGroups, id) 51 | 52 | return nil 53 | } 54 | 55 | // GetProxyFromProxyGroup gets a proxy from a group 56 | func GetProxyFromProxyGroup(id string) (*Proxy, error) { 57 | if !DoesProxyGroupExist(id) { 58 | return &Proxy{}, ProxyGroupDoesNotExistErr 59 | } 60 | 61 | proxyGroupMutex.Lock() 62 | defer proxyGroupMutex.Unlock() 63 | 64 | proxyGroup := proxyGroups[id] 65 | 66 | proxyIds := proxyGroup.Proxies.Keys() 67 | 68 | if len(proxyIds) == 0 { 69 | return &Proxy{}, ProxyGroupEmptyErr 70 | } 71 | 72 | firstProxyId := proxyIds[0] 73 | 74 | proxy, err := GetProxy(firstProxyId) 75 | 76 | if err != nil { 77 | return &Proxy{}, err 78 | } 79 | 80 | // remove proxy from list 81 | proxyGroup.Proxies.Delete(firstProxyId) 82 | 83 | // add proxy to the back of the list 84 | proxyGroup.Proxies.Set(firstProxyId, true) 85 | 86 | return proxy, nil 87 | } 88 | 89 | // GetProxyGroup gets a proxy group 90 | func GetProxyGroup(id string) (*ProxyGroup, error) { 91 | if !DoesProxyGroupExist(id) { 92 | return &ProxyGroup{}, ProxyGroupDoesNotExistErr 93 | } 94 | 95 | proxyGroupMutex.RLock() 96 | defer proxyGroupMutex.RUnlock() 97 | 98 | proxyGroup := proxyGroups[id] 99 | 100 | return proxyGroup, nil 101 | } 102 | 103 | // GetAllProxyGroupIds gets all proxy group ids 104 | func GetAllProxyGroupIds() []string { 105 | ids := make([]string, 0) 106 | 107 | for id := range proxyGroups { 108 | ids = append(ids, id) 109 | } 110 | 111 | return ids 112 | } 113 | -------------------------------------------------------------------------------- /internal/proxy/types.go: -------------------------------------------------------------------------------- 1 | package proxy 2 | 3 | import "github.com/iancoleman/orderedmap" 4 | 5 | type Proxy struct { 6 | URL string `json:"url"` 7 | } 8 | 9 | type ProxyGroup struct { 10 | ID string `json:"id"` 11 | Name string `json:"name"` 12 | Proxies *orderedmap.OrderedMap `json:"proxies"` 13 | } 14 | -------------------------------------------------------------------------------- /internal/task/task.go: -------------------------------------------------------------------------------- 1 | package task 2 | 3 | // NOTE: 4 | // there is a better way to handle tasks with interfaces 5 | 6 | import ( 7 | "errors" 8 | "github.com/EdwinJ0124/bot-base/internal/profile" 9 | "github.com/lithammer/shortuuid" 10 | "sync" 11 | ) 12 | 13 | var ( 14 | taskMutex = sync.RWMutex{} 15 | 16 | TaskDoesNotExistErr = errors.New("task does not exist") 17 | 18 | tasks = make(map[string]*Task) 19 | ) 20 | 21 | // DoesTaskExist checks if a task exists 22 | func DoesTaskExist(id string) bool { 23 | taskMutex.RLock() 24 | defer taskMutex.RUnlock() 25 | _, ok := tasks[id] 26 | return ok 27 | } 28 | 29 | // CreateTask creates a task 30 | func CreateTask(taskType, site, product string) string { 31 | taskMutex.Lock() 32 | defer taskMutex.Unlock() 33 | 34 | id := shortuuid.New() 35 | 36 | tasks[id] = &Task{ 37 | Type: taskType, 38 | Site: site, 39 | Product: product, 40 | } 41 | 42 | return id 43 | } 44 | 45 | // RemoveTask removes a task 46 | func RemoveTask(id string) error { 47 | if !DoesTaskExist(id) { 48 | return TaskDoesNotExistErr 49 | } 50 | 51 | taskMutex.Lock() 52 | defer taskMutex.Unlock() 53 | 54 | // stop the task if active 55 | task := tasks[id] 56 | task.Cancel() 57 | 58 | delete(tasks, id) 59 | 60 | return nil 61 | } 62 | 63 | // GetTask gets a task 64 | func GetTask(id string) (*Task, error) { 65 | if !DoesTaskExist(id) { 66 | return &Task{}, TaskDoesNotExistErr 67 | } 68 | 69 | taskMutex.RLock() 70 | defer taskMutex.RUnlock() 71 | 72 | return tasks[id], nil 73 | } 74 | 75 | // SetProfileGroupToTask sets a profile group to a task 76 | func SetProfileGroupToTask(taskId, profileGroupId string) error { 77 | if !DoesTaskExist(taskId) { 78 | return TaskDoesNotExistErr 79 | } 80 | 81 | if !profile.DoesProfileGroupExist(profileGroupId) { 82 | return profile.ProfileGroupDoesNotExistErr 83 | } 84 | 85 | taskMutex.Lock() 86 | defer taskMutex.Unlock() 87 | 88 | task := tasks[taskId] 89 | 90 | task.ProfileGroupID = profileGroupId 91 | 92 | return nil 93 | } 94 | 95 | // SetTaskToTaskGroup sets a task to a task group 96 | func SetTaskToTaskGroup(taskId, taskGroupId string) error { 97 | if !DoesTaskExist(taskId) { 98 | return TaskDoesNotExistErr 99 | } 100 | 101 | if !DoesTaskGroupExist(taskGroupId) { 102 | return TaskGroupDoesNotExistErr 103 | } 104 | 105 | taskGroupMutex.Lock() 106 | defer taskGroupMutex.Unlock() 107 | 108 | taskGroup := taskGroups[taskGroupId] 109 | 110 | taskGroup.Tasks[taskId] = true 111 | 112 | return nil 113 | } 114 | 115 | // RemoveTaskFromTaskGroup removes a task from a task group 116 | func RemoveTaskFromTaskGroup(taskId, taskGroupId string) error { 117 | if !DoesTaskExist(taskId) { 118 | return TaskDoesNotExistErr 119 | } 120 | 121 | if !DoesTaskGroupExist(taskGroupId) { 122 | return TaskGroupDoesNotExistErr 123 | } 124 | 125 | taskMutex.Lock() 126 | defer taskMutex.Unlock() 127 | 128 | taskGroup := taskGroups[taskGroupId] 129 | 130 | delete(taskGroup.Tasks, taskId) 131 | 132 | return nil 133 | } 134 | -------------------------------------------------------------------------------- /internal/task/task_group.go: -------------------------------------------------------------------------------- 1 | package task 2 | 3 | import ( 4 | "errors" 5 | "github.com/lithammer/shortuuid" 6 | "sync" 7 | ) 8 | 9 | var ( 10 | taskGroupMutex = sync.RWMutex{} 11 | 12 | TaskGroupDoesNotExistErr = errors.New("task group does not exist") 13 | 14 | taskGroups = make(map[string]*TaskGroup) 15 | ) 16 | 17 | // DoesTaskGroupExist determines if a task group is present 18 | func DoesTaskGroupExist(id string) bool { 19 | taskGroupMutex.RLock() 20 | defer taskGroupMutex.RUnlock() 21 | _, ok := taskGroups[id] 22 | return ok 23 | } 24 | 25 | // CreateTaskGroup creates a new task group 26 | func CreateTaskGroup(name string) string { 27 | taskGroupMutex.Lock() 28 | defer taskGroupMutex.Unlock() 29 | id := shortuuid.New() 30 | 31 | taskGroups[id] = &TaskGroup{ 32 | Name: name, 33 | ID: id, 34 | Tasks: make(map[string]bool), 35 | } 36 | 37 | return id 38 | } 39 | 40 | // RemoveTaskGroup removes a specified task group 41 | func RemoveTaskGroup(id string) error { 42 | if !DoesTaskGroupExist(id) { 43 | return TaskGroupDoesNotExistErr 44 | } 45 | 46 | taskGroupMutex.Lock() 47 | defer taskGroupMutex.Unlock() 48 | 49 | delete(taskGroups, id) 50 | 51 | return nil 52 | } 53 | 54 | // GetTaskGroup gets a task group from a specified id 55 | func GetTaskGroup(id string) (*TaskGroup, error) { 56 | if !DoesTaskGroupExist(id) { 57 | return &TaskGroup{}, TaskGroupDoesNotExistErr 58 | } 59 | 60 | taskGroupMutex.RLock() 61 | defer taskGroupMutex.RUnlock() 62 | 63 | return taskGroups[id], nil 64 | } 65 | 66 | // GetTaskIDs gets all task ids of a specified group 67 | func GetTaskIDs(id string) ([]string, error) { 68 | if !DoesTaskGroupExist(id) { 69 | return []string{}, TaskGroupDoesNotExistErr 70 | } 71 | 72 | taskGroupMutex.RLock() 73 | defer taskGroupMutex.RUnlock() 74 | 75 | ids := make([]string, 0) 76 | 77 | taskGroup := taskGroups[id] 78 | 79 | for id := range taskGroup.Tasks { 80 | ids = append(ids, id) 81 | } 82 | 83 | return ids, nil 84 | } 85 | 86 | // GetAllTaskGroupIDs gets all task group ids 87 | func GetAllTaskGroupIDs() []string { 88 | taskGroupMutex.RLock() 89 | defer taskGroupMutex.RUnlock() 90 | 91 | ids := make([]string, 0) 92 | 93 | for id := range taskGroups { 94 | ids = append(ids, id) 95 | } 96 | 97 | return ids 98 | } 99 | 100 | // GetAllTaskIDs gets all task ids 101 | func (t *TaskGroup) GetAllTaskIDs() []string { 102 | ids := make([]string, 0) 103 | 104 | for id := range t.Tasks { 105 | ids = append(ids, id) 106 | } 107 | 108 | return ids 109 | } 110 | 111 | // HasMonitors checks if task groups has monitors 112 | func (t *TaskGroup) HasMonitors() bool { 113 | return len(t.Monitors) > 0 114 | } 115 | 116 | // GetAllMonitorIDs gets all monitor ids 117 | func (t *TaskGroup) GetAllMonitorIDs() []string { 118 | ids := make([]string, 0) 119 | 120 | for id := range t.Monitors { 121 | ids = append(ids, id) 122 | } 123 | 124 | return ids 125 | } 126 | -------------------------------------------------------------------------------- /internal/task/type.go: -------------------------------------------------------------------------------- 1 | package task 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | ) 7 | 8 | var ( 9 | DoneTaskState TaskState = "done" 10 | ErrorTaskState TaskState = "error" 11 | 12 | TaskTypeDoesNotExistErr = errors.New("task type does not exist") 13 | TaskHandlerDoesNotExistErr = errors.New("task handler does not exist") 14 | 15 | taskTypes = make(map[string]*TaskType) 16 | ) 17 | 18 | // RegisterTaskType register task type 19 | func RegisterTaskType(taskType string) *TaskType { 20 | taskTypes[taskType] = &TaskType{ 21 | handlers: make(TaskReflectMap), 22 | } 23 | return taskTypes[taskType] 24 | } 25 | 26 | // DoesTaskTypeExist check if task type exists 27 | func DoesTaskTypeExist(taskType string) bool { 28 | _, ok := taskTypes[taskType] 29 | return ok 30 | } 31 | 32 | // GetTaskType gets a task type 33 | func GetTaskType(taskType string) (*TaskType, error) { 34 | if !DoesTaskTypeExist(taskType) { 35 | return &TaskType{}, TaskTypeDoesNotExistErr 36 | } 37 | return taskTypes[taskType], nil 38 | } 39 | 40 | // HasHandlers check if there are handlers 41 | func (t *TaskType) HasHandlers() bool { 42 | return len(t.handlers) > 0 43 | } 44 | 45 | func (t *TaskType) addHandler(handlerName TaskState, handler interface{}) { 46 | t.handlers[string(handlerName)] = reflect.ValueOf(handler) 47 | } 48 | 49 | // AddHandlers adds multiple handles to a task type 50 | func (t *TaskType) AddHandlers(handlers TaskHandlerMap) { 51 | for handlerName, handler := range handlers { 52 | if t.internalType == nil { 53 | handleTypes := reflect.TypeOf(handler) 54 | // func (t *task.Task, internal *SiteInternal) task.TaskState 55 | 56 | // we want the second one because the first one (0 index) will be task.Task type 57 | handleType := handleTypes.In(1) 58 | 59 | t.internalType = handleType 60 | } 61 | 62 | t.addHandler(handlerName, handler) 63 | } 64 | } 65 | 66 | // GetHandler gets a handler by handler name 67 | func (t *TaskType) GetHandler(handlerName TaskState) (reflect.Value, error) { 68 | handler, ok := t.handlers[string(handlerName)] 69 | 70 | if !ok { 71 | return reflect.Value{}, TaskHandlerDoesNotExistErr 72 | } 73 | 74 | return handler, nil 75 | } 76 | 77 | // GetFirstHandlerState gets the first handler state 78 | func (t *TaskType) GetFirstHandlerState() TaskState { 79 | return t.firstHandlerState 80 | } 81 | 82 | // SetFirstHandlerState sets the first handler state 83 | func (t *TaskType) SetFirstHandlerState(firstHandlerState TaskState) { 84 | t.firstHandlerState = firstHandlerState 85 | } 86 | 87 | // GetInternalType gets the internal type 88 | func (t *TaskType) GetInternalType() reflect.Type { 89 | return t.internalType 90 | } 91 | -------------------------------------------------------------------------------- /internal/task/types.go: -------------------------------------------------------------------------------- 1 | package task 2 | 3 | import ( 4 | "context" 5 | "github.com/EdwinJ0124/bot-base/third_party/hclient" 6 | "reflect" 7 | ) 8 | 9 | type Task struct { 10 | ID string `json:"id"` 11 | Type string `json:"type"` 12 | Site string `json:"site"` 13 | Product string `json:"product"` 14 | ProfileGroupID string `json:"profileGroupID"` 15 | ProxyListID string `json:"proxyListID"` 16 | Context context.Context `json:"-"` 17 | Cancel context.CancelFunc `json:"-"` 18 | Internal interface{} `json:"-"` 19 | Client *hclient.Client `json:"-"` 20 | Active bool `json:"-"` 21 | MonitorData chan interface{} `json:"-"` 22 | } 23 | 24 | type TaskGroup struct { 25 | ID string `json:"id"` 26 | Name string `json:"name"` 27 | Monitors map[string]bool `json:"monitorId"` 28 | Tasks map[string]bool `json:"tasks"` 29 | } 30 | 31 | type TaskType struct { 32 | firstHandlerState TaskState 33 | internalType reflect.Type 34 | handlers TaskReflectMap 35 | } 36 | 37 | type TaskState string 38 | type TaskHandlerMap map[TaskState]interface{} 39 | type TaskReflectMap map[string]reflect.Value 40 | -------------------------------------------------------------------------------- /internal/task_manager/task_manager.go: -------------------------------------------------------------------------------- 1 | package taskmngr 2 | 3 | import ( 4 | "context" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | "reflect" 7 | "time" 8 | ) 9 | 10 | func handleTaskState(taskState task.TaskState, taskType *task.TaskType, t *task.Task) task.TaskState { 11 | nextTaskHandler, err := taskType.GetHandler(taskState) 12 | 13 | if err != nil { 14 | return task.ErrorTaskState 15 | } 16 | 17 | // func (t *task.Task, internal *SiteInternal) task.TaskState 18 | nextNextTaskType := nextTaskHandler.Call([]reflect.Value{reflect.ValueOf(t), reflect.ValueOf(t.Internal)}) 19 | 20 | return task.TaskState(nextNextTaskType[0].String()) 21 | } 22 | 23 | // RunTask starts a task 24 | func RunTask(t *task.Task) { 25 | t.Context, t.Cancel = context.WithCancel(context.Background()) 26 | t.Active = true 27 | 28 | defer func() { 29 | if r := recover(); r != nil { 30 | // handle crash 31 | } 32 | }() 33 | 34 | if !task.DoesTaskTypeExist(t.Type) { 35 | return 36 | } 37 | 38 | taskType, err := task.GetTaskType(t.Type) 39 | 40 | if err != nil { 41 | t.Active = false 42 | return 43 | } 44 | 45 | hasHandlers := taskType.HasHandlers() 46 | 47 | if !hasHandlers { 48 | t.Active = false 49 | return 50 | } 51 | 52 | nextState := taskType.GetFirstHandlerState() 53 | 54 | if len(nextState) == 0 { 55 | t.Active = false 56 | return 57 | } 58 | 59 | t.Internal = reflect.New(taskType.GetInternalType().Elem()).Interface() 60 | 61 | // loop the task states 62 | for { 63 | nextState = handleTaskState(nextState, taskType, t) 64 | 65 | if nextState == task.DoneTaskState || t.Context.Err() != nil { 66 | // you can report that the task stopped here 67 | t.Active = false 68 | break 69 | } else if nextState == task.ErrorTaskState { 70 | // report errors 71 | t.Active = false 72 | break 73 | } 74 | 75 | time.Sleep(1 * time.Millisecond) 76 | } 77 | } 78 | 79 | // StopTask stops a task 80 | func StopTask(t *task.Task) { 81 | t.Cancel() 82 | } 83 | -------------------------------------------------------------------------------- /internal/utils/address.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | var CountryToISO = map[string]string{ 4 | "United States": "US", 5 | } 6 | 7 | var StateToISO = map[string]string{ 8 | "Alabama": "AL", 9 | "Alaska": "AK", 10 | "American Samoa": "AS", 11 | "Arizona": "AZ", 12 | "Arkansas": "AR", 13 | "Armed Forces Americas": "AA", 14 | "Armed Forces Europe": "AE", 15 | "Armed Forces Pacific": "AP", 16 | "California": "CA", 17 | "Colorado": "CO", 18 | "Connecticut": "CT", 19 | "Delaware": "DE", 20 | "District of Columbia": "DC", 21 | "Federated States of Micronesia": "FM", 22 | "Florida": "FL", 23 | "Georgia": "GA", 24 | "Guam": "GU", 25 | "Hawaii": "HI", 26 | "Idaho": "ID", 27 | "Illinois": "IL", 28 | "Indiana": "IN", 29 | "Iowa": "IA", 30 | "Kansas": "KS", 31 | "Kentucky": "KY", 32 | "Louisiana": "LA", 33 | "Maine": "ME", 34 | "Marshall Islands": "MH", 35 | "Maryland": "MD", 36 | "Massachusetts": "MA", 37 | "Michigan": "MI", 38 | "Minnesota": "MN", 39 | "Mississippi": "MS", 40 | "Missouri": "MO", 41 | "Montana": "MT", 42 | "Nebraska": "NE", 43 | "Nevada": "NV", 44 | "New Hampshire": "NH", 45 | "New Jersey": "NJ", 46 | "New Mexico": "NM", 47 | "New York": "NY", 48 | "North Carolina": "NC", 49 | "North Dakota": "ND", 50 | "Northern Mariana Islands": "MP", 51 | "Ohio": "OH", 52 | "Oklahoma": "OK", 53 | "Oregon": "OR", 54 | "Palau": "PW", 55 | "Pennsylvania": "PA", 56 | "Puerto Rico": "PR", 57 | "Rhode Island": "RI", 58 | "South Carolina": "SC", 59 | "South Dakota": "SD", 60 | "Tennessee": "TN", 61 | "Texas": "TX", 62 | "Utah": "UT", 63 | "Vermont": "VT", 64 | "Virgin Islands": "VI", 65 | "Virginia": "VA", 66 | "Washington": "WA", 67 | "West Virginia": "WV", 68 | "Wisconsin": "WI", 69 | "Wyoming": "WY", 70 | } 71 | -------------------------------------------------------------------------------- /monitors/dummy/dummy_monitor.go: -------------------------------------------------------------------------------- 1 | package dummymntr 2 | -------------------------------------------------------------------------------- /monitors/footsites/footsites.go: -------------------------------------------------------------------------------- 1 | package footsitesmntr 2 | 3 | import ( 4 | "github.com/EdwinJ0124/bot-base/internal/monitor" 5 | ) 6 | 7 | func Initialize() { 8 | monitorType := monitor.RegisterMonitorType("footsites") 9 | 10 | monitorType.SetFirstHandlerState(INITIALIZE) 11 | 12 | monitorType.AddHandlers(monitor.MonitorHandlerMap{ 13 | INITIALIZE: initialize, 14 | GET_STOCK: getStock, 15 | }) 16 | } 17 | -------------------------------------------------------------------------------- /monitors/footsites/initialize.go: -------------------------------------------------------------------------------- 1 | package footsitesmntr 2 | 3 | import "github.com/EdwinJ0124/bot-base/internal/monitor" 4 | 5 | func initialize(m *monitor.Monitor, internal *FootsitesMonitorInternal) monitor.MonitorState { 6 | switch m.Site { 7 | case "footlocker": 8 | internal.Host = "www.footlocker.com" 9 | case "footaction": 10 | internal.Host = "www.footaction.com" 11 | case "eastbay": 12 | internal.Host = "www.eastbay.com" 13 | case "champssports": 14 | internal.Host = "www.champssports.com" 15 | case "footlockerca": 16 | internal.Host = "www.footlocker.ca" 17 | default: 18 | internal.Host = "www.footlocker.com" 19 | } 20 | 21 | return monitor.DoneMonitorState 22 | } 23 | -------------------------------------------------------------------------------- /monitors/footsites/states.go: -------------------------------------------------------------------------------- 1 | package footsitesmntr 2 | 3 | import ( 4 | "github.com/EdwinJ0124/bot-base/internal/monitor" 5 | ) 6 | 7 | var ( 8 | INITIALIZE monitor.MonitorState = "initialize" 9 | GET_STOCK monitor.MonitorState = "get_stock" 10 | ) 11 | -------------------------------------------------------------------------------- /monitors/footsites/stock.go: -------------------------------------------------------------------------------- 1 | package footsitesmntr 2 | 3 | import "github.com/EdwinJ0124/bot-base/internal/monitor" 4 | 5 | func getStock(m *monitor.Monitor, internal *FootsitesMonitorInternal) monitor.MonitorState { 6 | 7 | // send a request to 8 | 9 | return HandleStockResponse(m, internal) 10 | } 11 | 12 | func HandleStockResponse(m *monitor.Monitor, internal *FootsitesMonitorInternal) monitor.MonitorState { 13 | // once in stock, you can simple notify all tasks associated with this monitor by doing 14 | 15 | //m.NotifyTasks(&FootsitesMonitorData{ 16 | // // necessary data needed to be sent 17 | //}) 18 | 19 | return GET_STOCK 20 | } 21 | -------------------------------------------------------------------------------- /monitors/footsites/types.go: -------------------------------------------------------------------------------- 1 | package footsitesmntr 2 | 3 | type FootsitesMonitorInternal struct { 4 | Host string 5 | } 6 | type FootsitesMonitorData struct { 7 | } 8 | -------------------------------------------------------------------------------- /sites/dummy_site/dummy_site.go: -------------------------------------------------------------------------------- 1 | package dummysite 2 | -------------------------------------------------------------------------------- /sites/footsites/billing.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | "github.com/EdwinJ0124/bot-base/internal/utils" 7 | "strings" 8 | ) 9 | 10 | func submitBilling(t *task.Task, internal *FootsitesInternal) task.TaskState { 11 | requestBody := BillingRequest{ 12 | Loqatesearch: "", 13 | Country: Country{ 14 | Isocode: utils.CountryToISO[internal.Profile.ShippingAddress.Country], 15 | Name: internal.Profile.ShippingAddress.Country, 16 | }, 17 | Email: false, 18 | Firstname: strings.Split(internal.Profile.ShippingAddress.Name, " ")[0], 19 | ID: nil, 20 | Lastname: strings.Split(internal.Profile.ShippingAddress.Name, " ")[1], 21 | Line1: internal.Profile.ShippingAddress.Line1, 22 | Line2: internal.Profile.ShippingAddress.Line2, 23 | Phone: internal.Profile.ShippingAddress.Phone, 24 | Postalcode: internal.Profile.ShippingAddress.Postcode, 25 | Recordtype: "S", 26 | Region: Region{ 27 | Countryiso: utils.CountryToISO[internal.Profile.ShippingAddress.Country], 28 | Isocode: fmt.Sprintf("%s:%s", utils.CountryToISO[internal.Profile.ShippingAddress.Country], utils.StateToISO[internal.Profile.ShippingAddress.State]), 29 | Isocodeshort: utils.StateToISO[internal.Profile.ShippingAddress.State], 30 | Name: internal.Profile.ShippingAddress.State, 31 | }, 32 | Regionfpo: nil, 33 | Saveinaddressbook: false, 34 | Setasbilling: false, 35 | Setasdefaultshipping: false, 36 | Setasdefaultbilling: false, 37 | Shippingaddress: true, 38 | Visibleinaddressbook: false, 39 | Town: internal.Profile.ShippingAddress.City, 40 | Type: "default", 41 | } 42 | 43 | _, err := t.Client.NewRequest(). 44 | SetURL(fmt.Sprintf("https://%s/api/users/carts/current/set-billing", internal.Host)). 45 | SetMethod("POST"). 46 | SetHeader("user-agent", userAgent). 47 | SetHeader("accept", "application/json"). 48 | SetHeader("content-type", "application/json"). 49 | SetJSONBody(requestBody). 50 | Do() 51 | 52 | if err != nil { 53 | // handle error and retry 54 | return SUBMIT_BILLING 55 | } 56 | 57 | return handleSubmitBillingResponse(t) 58 | } 59 | 60 | func handleSubmitBillingResponse(t *task.Task) task.TaskState { 61 | if t.Client.LatestResponse.StatusCode() > 201 { 62 | // message := HandleStatusCodes(resp.StatusCode()) 63 | 64 | // handle error and retry 65 | return SUBMIT_BILLING 66 | } 67 | 68 | return SUBMIT_ORDER 69 | } 70 | -------------------------------------------------------------------------------- /sites/footsites/cart.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | ) 7 | 8 | func addToCart(t *task.Task, internal *FootsitesInternal) task.TaskState { 9 | requestBody := AddToCartRequest{ 10 | ProductQuantity: 1, 11 | ProductID: internal.VariantID, 12 | } 13 | 14 | _, err := t.Client.NewRequest(). 15 | SetURL(fmt.Sprintf("https://%s/api/users/carts/current/entries", internal.Host)). 16 | SetMethod("POST"). 17 | SetHeader("user-agent", userAgent). 18 | SetHeader("accept", "application/json"). 19 | SetHeader("content-type", "application/json"). 20 | SetJSONBody(requestBody). 21 | Do() 22 | 23 | if err != nil { 24 | // handle error and retry 25 | return ADD_TO_CART 26 | } 27 | 28 | return handleAddToCartResponse(t) 29 | } 30 | 31 | func handleAddToCartResponse(t *task.Task) task.TaskState { 32 | if t.Client.LatestResponse.StatusCode() > 201 { 33 | // message := HandleStatusCodes(resp.StatusCode()) 34 | 35 | // handle error and retry 36 | return ADD_TO_CART 37 | } 38 | 39 | return VERIFY_EMAIL 40 | } 41 | -------------------------------------------------------------------------------- /sites/footsites/email.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/profile" 6 | "github.com/EdwinJ0124/bot-base/internal/task" 7 | ) 8 | 9 | func verifyEmail(t *task.Task, internal *FootsitesInternal) task.TaskState { 10 | // get profile data 11 | if !internal.ProfileRetrieved { 12 | 13 | // profile is in a queue system 14 | profile, err := profile.GetProfileFromProfileGroup(t.ProfileGroupID) 15 | 16 | if err != nil { 17 | // handle error and retry 18 | return VERIFY_EMAIL 19 | } 20 | 21 | internal.Profile = profile 22 | internal.ProfileRetrieved = true 23 | } 24 | 25 | _, err := t.Client.NewRequest(). 26 | SetURL(fmt.Sprintf("https://%s/api/users/carts/current/email/%s", internal.Host, internal.Profile.ShippingAddress.Email)). 27 | SetMethod("PUT"). 28 | SetHeader("user-agent", userAgent). 29 | SetHeader("accept", "application/json"). 30 | SetHeader("content-type", "application/json"). 31 | Do() 32 | 33 | if err != nil { 34 | // handle error and retry 35 | return VERIFY_EMAIL 36 | } 37 | 38 | return handleVerifyEmailResponse(t) 39 | } 40 | 41 | func handleVerifyEmailResponse(t *task.Task) task.TaskState { 42 | if t.Client.LatestResponse.StatusCode() > 201 { 43 | // message := HandleStatusCodes(resp.StatusCode()) 44 | 45 | // handle error and retry 46 | return VERIFY_EMAIL 47 | } 48 | 49 | return SUBMIT_SHIPPING 50 | } 51 | -------------------------------------------------------------------------------- /sites/footsites/footsites.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import "github.com/EdwinJ0124/bot-base/internal/task" 4 | 5 | var ( 6 | userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" 7 | ) 8 | 9 | func Initialize() { 10 | taskType := task.RegisterTaskType("footsites") 11 | 12 | taskType.SetFirstHandlerState(INITIALIZE) 13 | 14 | taskType.AddHandlers(task.TaskHandlerMap{ 15 | INITIALIZE: initialize, 16 | GET_SESSION: getSession, 17 | WAIT_FOR_MONITOR: waitForMonitor, 18 | ADD_TO_CART: addToCart, 19 | VERIFY_EMAIL: verifyEmail, 20 | SUBMIT_SHIPPING: submitShipping, 21 | SUBMIT_BILLING: submitBilling, 22 | SUBMIT_ORDER: submitOrder, 23 | }) 24 | } 25 | -------------------------------------------------------------------------------- /sites/footsites/initialize.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "github.com/EdwinJ0124/bot-base/internal/proxy" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | "github.com/EdwinJ0124/bot-base/third_party/hclient" 7 | ) 8 | 9 | // NOTE: 10 | // there are better ways to do it via a new task system, but this works too 11 | 12 | func initialize(t *task.Task, internal *FootsitesInternal) task.TaskState { 13 | switch t.Site { 14 | case "footlocker": 15 | internal.Host = "www.footlocker.com" 16 | case "footaction": 17 | internal.Host = "www.footaction.com" 18 | case "eastbay": 19 | internal.Host = "www.eastbay.com" 20 | case "champssports": 21 | internal.Host = "www.champssports.com" 22 | case "footlockerca": 23 | internal.Host = "www.footlocker.ca" 24 | default: 25 | internal.Host = "www.footlocker.com" 26 | } 27 | 28 | proxyData, err := proxy.GetProxyFromProxyGroup(t.ProxyListID) 29 | 30 | if err != nil { 31 | return task.ErrorTaskState 32 | } 33 | 34 | client, err := hclient.NewClient(proxyData.URL, internal.Host) 35 | 36 | if err != nil { 37 | return task.ErrorTaskState 38 | } 39 | 40 | t.Client = client 41 | 42 | return GET_SESSION 43 | } 44 | -------------------------------------------------------------------------------- /sites/footsites/monitor.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "github.com/EdwinJ0124/bot-base/internal/task" 5 | ) 6 | 7 | func waitForMonitor(t *task.Task, internal *FootsitesInternal) task.TaskState { 8 | // wait for data to come from montitor 9 | 10 | // data := <- t.MonitorData 11 | 12 | // monitorData := data.(*footsites.FootsitesMonitorData) 13 | 14 | // handle data 15 | 16 | return ADD_TO_CART 17 | } 18 | -------------------------------------------------------------------------------- /sites/footsites/order.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | ) 7 | 8 | func submitOrder(t *task.Task, internal *FootsitesInternal) task.TaskState { 9 | requestBody := OrderRequest{ 10 | Cartid: "", 11 | Deviceid: "", 12 | Encryptedcardnumber: "", 13 | Encryptedexpirymonth: "", 14 | Encryptedexpiryyear: "", 15 | Encryptedsecuritycode: "", 16 | Paymentmethod: "CREDITCARD", 17 | Preferredlanguage: "en", 18 | Returnurl: fmt.Sprintf("https://%s/adyen/checkout", internal.Host), 19 | Termsandcondition: false, 20 | } 21 | 22 | _, err := t.Client.NewRequest(). 23 | SetURL(fmt.Sprintf("https://%s/api/v2/users/orders", internal.Host)). 24 | SetMethod("POST"). 25 | SetHeader("user-agent", userAgent). 26 | SetHeader("accept", "application/json"). 27 | SetHeader("content-type", "application/json"). 28 | SetJSONBody(requestBody). 29 | Do() 30 | 31 | if err != nil { 32 | // handle error and retry 33 | return SUBMIT_ORDER 34 | } 35 | 36 | return handleSubmitOrderResponse(t) 37 | } 38 | 39 | func handleSubmitOrderResponse(t *task.Task) task.TaskState { 40 | if t.Client.LatestResponse.StatusCode() > 201 { 41 | // message := HandleStatusCodes(resp.StatusCode()) 42 | 43 | // handle error and retry 44 | return SUBMIT_ORDER 45 | } 46 | 47 | return task.DoneTaskState 48 | } 49 | -------------------------------------------------------------------------------- /sites/footsites/session.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | ) 7 | 8 | func getSession(t *task.Task, internal *FootsitesInternal) task.TaskState { 9 | _, err := t.Client.NewRequest(). 10 | SetURL(fmt.Sprintf("https://%s/api/session", internal.Host)). 11 | SetMethod("GET"). 12 | SetHeader("user-agent", userAgent). 13 | SetHeader("accept", "application/json"). 14 | Do() 15 | 16 | if err != nil { 17 | // handle error and retry 18 | return GET_SESSION 19 | } 20 | 21 | return handleSessionResponse(t, internal) 22 | } 23 | 24 | func handleSessionResponse(t *task.Task, internal *FootsitesInternal) task.TaskState { 25 | if t.Client.LatestResponse.StatusCode() > 201 { 26 | // message := HandleStatusCodes(resp.StatusCode()) 27 | 28 | // handle error and retry 29 | return GET_SESSION 30 | } 31 | 32 | sessionResponse := SessionResponse{} 33 | 34 | err := t.Client.LatestResponse.BodyAsJSON(&sessionResponse) 35 | 36 | if err != nil { 37 | // handle error and retry 38 | return GET_SESSION 39 | } 40 | 41 | internal.CSRFToken = sessionResponse.Data.CSRFToken 42 | 43 | return WAIT_FOR_MONITOR 44 | } 45 | -------------------------------------------------------------------------------- /sites/footsites/shipping.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import ( 4 | "fmt" 5 | "github.com/EdwinJ0124/bot-base/internal/task" 6 | "github.com/EdwinJ0124/bot-base/internal/utils" 7 | "strings" 8 | ) 9 | 10 | func submitShipping(t *task.Task, internal *FootsitesInternal) task.TaskState { 11 | requestBody := ShippingRequest{ 12 | Shippingaddress: ShippingAddress{ 13 | Loqatesearch: "", 14 | Country: Country{ 15 | Isocode: utils.CountryToISO[internal.Profile.ShippingAddress.Country], 16 | Name: internal.Profile.ShippingAddress.Country, 17 | }, 18 | Email: false, 19 | Firstname: strings.Split(internal.Profile.ShippingAddress.Name, " ")[0], 20 | ID: nil, 21 | Lastname: strings.Split(internal.Profile.ShippingAddress.Name, " ")[1], 22 | Line1: internal.Profile.ShippingAddress.Line1, 23 | Line2: internal.Profile.ShippingAddress.Line2, 24 | Phone: internal.Profile.ShippingAddress.Phone, 25 | Postalcode: internal.Profile.ShippingAddress.Postcode, 26 | Recordtype: "S", 27 | Region: Region{ 28 | Countryiso: utils.CountryToISO[internal.Profile.ShippingAddress.Country], 29 | Isocode: fmt.Sprintf("%s:%s", utils.CountryToISO[internal.Profile.ShippingAddress.Country], utils.StateToISO[internal.Profile.ShippingAddress.State]), 30 | Isocodeshort: utils.StateToISO[internal.Profile.ShippingAddress.State], 31 | Name: internal.Profile.ShippingAddress.State, 32 | }, 33 | Regionfpo: nil, 34 | Saveinaddressbook: false, 35 | Setasbilling: true, 36 | Setasdefaultshipping: false, 37 | Setasdefaultbilling: false, 38 | Shippingaddress: true, 39 | Town: internal.Profile.ShippingAddress.City, 40 | Type: "default", 41 | }, 42 | } 43 | 44 | _, err := t.Client.NewRequest(). 45 | SetURL(fmt.Sprintf("https://%s/api/users/carts/current/addresses/shipping", internal.Host)). 46 | SetMethod("POST"). 47 | SetHeader("user-agent", userAgent). 48 | SetHeader("accept", "application/json"). 49 | SetHeader("content-type", "application/json"). 50 | SetJSONBody(requestBody). 51 | Do() 52 | 53 | if err != nil { 54 | // handle error and retry 55 | return SUBMIT_SHIPPING 56 | } 57 | 58 | return handleSubmitShippingResponse(t) 59 | } 60 | 61 | func handleSubmitShippingResponse(t *task.Task) task.TaskState { 62 | if t.Client.LatestResponse.StatusCode() > 201 { 63 | // message := HandleStatusCodes(resp.StatusCode()) 64 | 65 | // handle error and retry 66 | return SUBMIT_SHIPPING 67 | } 68 | 69 | return SUBMIT_BILLING 70 | } 71 | -------------------------------------------------------------------------------- /sites/footsites/states.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import "github.com/EdwinJ0124/bot-base/internal/task" 4 | 5 | var ( 6 | INITIALIZE task.TaskState = "initialize" 7 | GET_SESSION task.TaskState = "get_session" 8 | WAIT_FOR_MONITOR task.TaskState = "wait_for_monitor" 9 | ADD_TO_CART task.TaskState = "add_to_cart" 10 | VERIFY_EMAIL task.TaskState = "verify_email" 11 | SUBMIT_SHIPPING task.TaskState = "submit_shipping" 12 | SUBMIT_BILLING task.TaskState = "submit_billing" 13 | SUBMIT_ORDER task.TaskState = "submit_order" 14 | ) 15 | -------------------------------------------------------------------------------- /sites/footsites/types.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import "github.com/EdwinJ0124/bot-base/internal/profile" 4 | 5 | type SessionResponse struct { 6 | Data struct { 7 | CSRFToken string `json:"csrfToken"` 8 | } `json:"data"` 9 | } 10 | 11 | type AddToCartRequest struct { 12 | ProductQuantity int `json:"productQuantity"` 13 | ProductID string `json:"productId"` 14 | } 15 | 16 | type Country struct { 17 | Isocode string `json:"isocode"` 18 | Name string `json:"name"` 19 | } 20 | 21 | type Region struct { 22 | Countryiso string `json:"countryIso"` 23 | Isocode string `json:"isocode"` 24 | Isocodeshort string `json:"isocodeShort"` 25 | Name string `json:"name"` 26 | } 27 | 28 | type ShippingAddress struct { 29 | Setasdefaultbilling bool `json:"setAsDefaultBilling"` 30 | Setasdefaultshipping bool `json:"setAsDefaultShipping"` 31 | Firstname string `json:"firstName"` 32 | Lastname string `json:"lastName"` 33 | Email bool `json:"email"` 34 | Phone string `json:"phone"` 35 | Country Country `json:"country"` 36 | ID interface{} `json:"id"` 37 | Setasbilling bool `json:"setAsBilling"` 38 | Saveinaddressbook bool `json:"saveInAddressBook"` 39 | Region Region `json:"region"` 40 | Type string `json:"type"` 41 | Loqatesearch string `json:"LoqateSearch"` 42 | Line1 string `json:"line1"` 43 | Line2 string `json:"line2"` 44 | Postalcode string `json:"postalCode"` 45 | Town string `json:"town"` 46 | Regionfpo interface{} `json:"regionFPO"` 47 | Shippingaddress bool `json:"shippingAddress"` 48 | Recordtype string `json:"recordType"` 49 | } 50 | 51 | type BillingRequest struct { 52 | Setasdefaultbilling bool `json:"setAsDefaultBilling"` 53 | Setasdefaultshipping bool `json:"setAsDefaultShipping"` 54 | Firstname string `json:"firstName"` 55 | Lastname string `json:"lastName"` 56 | Email bool `json:"email"` 57 | Phone string `json:"phone"` 58 | Country Country `json:"country"` 59 | ID interface{} `json:"id"` 60 | Setasbilling bool `json:"setAsBilling"` 61 | Saveinaddressbook bool `json:"saveInAddressBook"` 62 | Region Region `json:"region"` 63 | Type string `json:"type"` 64 | Loqatesearch string `json:"LoqateSearch"` 65 | Line1 string `json:"line1"` 66 | Line2 string `json:"line2"` 67 | Postalcode string `json:"postalCode"` 68 | Town string `json:"town"` 69 | Regionfpo interface{} `json:"regionFPO"` 70 | Shippingaddress bool `json:"shippingAddress"` 71 | Recordtype string `json:"recordType"` 72 | Visibleinaddressbook bool `json:"visibleInAddressBook"` 73 | } 74 | 75 | type ShippingRequest struct { 76 | Shippingaddress ShippingAddress `json:"shippingAddress"` 77 | } 78 | 79 | type BrowserInfo struct { 80 | Screenwidth int `json:"screenWidth"` 81 | Screenheight int `json:"screenHeight"` 82 | Colordepth int `json:"colorDepth"` 83 | Useragent string `json:"userAgent"` 84 | Timezoneoffset int `json:"timeZoneOffset"` 85 | Language string `json:"language"` 86 | Javaenabled bool `json:"javaEnabled"` 87 | } 88 | 89 | type OrderRequest struct { 90 | Preferredlanguage string `json:"preferredLanguage"` 91 | Termsandcondition bool `json:"termsAndCondition"` 92 | Deviceid string `json:"deviceId"` 93 | Cartid string `json:"cartId"` 94 | Encryptedcardnumber string `json:"encryptedCardNumber"` 95 | Encryptedexpirymonth string `json:"encryptedExpiryMonth"` 96 | Encryptedexpiryyear string `json:"encryptedExpiryYear"` 97 | Encryptedsecuritycode string `json:"encryptedSecurityCode"` 98 | Paymentmethod string `json:"paymentMethod"` 99 | Returnurl string `json:"returnUrl"` 100 | Browserinfo BrowserInfo `json:"browserInfo"` 101 | } 102 | 103 | type FootsitesInternal struct { 104 | // internal things 105 | Host string 106 | 107 | CSRFToken string 108 | VariantID string 109 | 110 | ProfileRetrieved bool 111 | Profile *profile.Profile 112 | } 113 | -------------------------------------------------------------------------------- /sites/footsites/util.go: -------------------------------------------------------------------------------- 1 | package footsites 2 | 3 | import "fmt" 4 | 5 | func HandleStatusCodes(statusCode int) string { 6 | switch statusCode { 7 | case 400: 8 | return "Bad Request" 9 | case 403: 10 | return "Banned" 11 | case 429: 12 | return "Rate Limited" 13 | case 529: 14 | return "Queue" 15 | case 531: 16 | return "Out of Stock" 17 | default: 18 | return fmt.Sprintf("%d", statusCode) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /third_party/hclient/client.go: -------------------------------------------------------------------------------- 1 | package hclient 2 | 3 | import ( 4 | "crypto/tls" 5 | "errors" 6 | "io" 7 | "net/http" 8 | "net/url" 9 | ) 10 | 11 | var ( 12 | NoCookieJarErr = errors.New("no cookie jar in client") 13 | ) 14 | 15 | // NewClient creates a new http client 16 | // Takes in the optional arguments: proxy, servername 17 | func NewClient(parameters ...string) (*Client, error) { 18 | tlsClientConfig := &tls.Config{ 19 | InsecureSkipVerify: true, 20 | } 21 | 22 | // parameters[0] = proxy 23 | // parameters[1] = sni 24 | if len(parameters) > 1 && len(parameters[1]) > 0 { 25 | tlsClientConfig.ServerName = parameters[1] 26 | } 27 | 28 | transport := &http.Transport{ 29 | ForceAttemptHTTP2: true, 30 | TLSClientConfig: tlsClientConfig, 31 | } 32 | 33 | if len(parameters) > 0 && len(parameters[0]) > 0 { 34 | proxyUrl, _ := url.Parse(parameters[0]) 35 | 36 | transport.Proxy = http.ProxyURL(proxyUrl) 37 | } 38 | 39 | return &Client{ 40 | client: &http.Client{ 41 | Transport: transport, 42 | }, 43 | LatestResponse: &Response{}, 44 | }, nil 45 | } 46 | 47 | // NewRequest creates a new request under a specified http client 48 | func (c *Client) NewRequest() *Request { 49 | return &Request{ 50 | client: c, 51 | header: make(http.Header), 52 | } 53 | } 54 | 55 | // AddCookie adds a new cookie to the request client cookie jar 56 | func (c *Client) AddCookie(u *url.URL, cookie *http.Cookie) error { 57 | if c.client.Jar == nil { 58 | return NoCookieJarErr 59 | } 60 | 61 | c.client.Jar.SetCookies(u, []*http.Cookie{cookie}) 62 | 63 | return nil 64 | } 65 | 66 | // RemoveCookie removes the specified cookie from the request client cookie jar 67 | func (c *Client) RemoveCookie(u *url.URL, cookie string) error { 68 | if c.client.Jar == nil { 69 | return NoCookieJarErr 70 | } 71 | 72 | newCookie := &http.Cookie{ 73 | Name: cookie, 74 | Value: "", 75 | } 76 | 77 | c.client.Jar.SetCookies(u, []*http.Cookie{newCookie}) 78 | 79 | return nil 80 | } 81 | 82 | // Do will send the specified request 83 | func (c *Client) Do(r *http.Request) (*Response, error) { 84 | resp, err := c.client.Do(r) 85 | if err != nil { 86 | return nil, err 87 | } 88 | 89 | body, err := io.ReadAll(resp.Body) 90 | _ = resp.Body.Close() 91 | if err != nil { 92 | return nil, err 93 | } 94 | 95 | r.Close = true 96 | 97 | response := &Response{ 98 | headers: resp.Header, 99 | body: body, 100 | status: resp.Status, 101 | statusCode: resp.StatusCode, 102 | } 103 | 104 | c.LatestResponse = response 105 | 106 | return response, nil 107 | } 108 | -------------------------------------------------------------------------------- /third_party/hclient/request.go: -------------------------------------------------------------------------------- 1 | package hclient 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "net/http" 7 | "net/url" 8 | "strings" 9 | ) 10 | 11 | // SetURL sets the url of the request 12 | func (r *Request) SetURL(url string) *Request { 13 | r.url = url 14 | return r 15 | } 16 | 17 | // SetMethod sets the method of the request 18 | func (r *Request) SetMethod(method string) *Request { 19 | r.method = method 20 | return r 21 | } 22 | 23 | // AddHeader adds a specified header to the request 24 | // If the header already exists, the value will be appended by the new specified value 25 | // If the header does not exist, the header will be set to the specified value 26 | func (r *Request) AddHeader(key, value string) *Request { 27 | if header, ok := r.header[key]; ok { 28 | header = append(header, value) 29 | r.header[key] = header 30 | } else { 31 | r.header[key] = []string{value} 32 | } 33 | return r 34 | } 35 | 36 | // SetHeader sets a specified header to the request 37 | // This overrides any previously set values of the specified header 38 | func (r *Request) SetHeader(key, value string) *Request { 39 | r.header[key] = []string{value} 40 | return r 41 | } 42 | 43 | // SetHost sets the host of the request 44 | func (r *Request) SetHost(value string) *Request { 45 | r.host = value 46 | return r 47 | } 48 | 49 | // SetJSONBody sets the body to a json value 50 | func (r *Request) SetJSONBody(body interface{}) *Request { 51 | b, _ := json.Marshal(body) 52 | r.body = bytes.NewBuffer(b) 53 | return r 54 | } 55 | 56 | // SetFormBody sets the body to a form value 57 | func (r *Request) SetFormBody(body url.Values) *Request { 58 | r.body = strings.NewReader(body.Encode()) 59 | return r 60 | } 61 | 62 | // Do will send the request with all specified request values 63 | func (r *Request) Do() (*Response, error) { 64 | req, err := http.NewRequest(r.method, r.url, r.body) 65 | if err != nil { 66 | return nil, err 67 | } 68 | 69 | for _, cookie := range r.cookies { 70 | if cookie != nil { 71 | req.AddCookie(cookie) 72 | } 73 | } 74 | 75 | req.Header = r.header 76 | 77 | if len(r.host) > 0 { 78 | req.Host = r.host 79 | } 80 | 81 | return r.client.Do(req) 82 | } 83 | -------------------------------------------------------------------------------- /third_party/hclient/response.go: -------------------------------------------------------------------------------- 1 | package hclient 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | ) 7 | 8 | // Header returns the response headers 9 | func (r *Response) Header() http.Header { 10 | return r.headers 11 | } 12 | 13 | // Body returns the response body 14 | func (r *Response) Body() []byte { 15 | return r.body 16 | } 17 | 18 | // BodyAsString returns the response body as a string 19 | func (r *Response) BodyAsString() string { 20 | return string(r.body) 21 | } 22 | 23 | // BodyAsJSON unmarshalls the current response body to the specified data structure 24 | func (r *Response) BodyAsJSON(data interface{}) error { 25 | return json.Unmarshal(r.body, data) 26 | } 27 | 28 | // Status returns the response status 29 | func (r *Response) Status() string { 30 | return r.status 31 | } 32 | 33 | // StatusCode returns the response status code 34 | func (r *Response) StatusCode() int { 35 | return r.statusCode 36 | } 37 | -------------------------------------------------------------------------------- /third_party/hclient/types.go: -------------------------------------------------------------------------------- 1 | package hclient 2 | 3 | import ( 4 | "io" 5 | "net/http" 6 | ) 7 | 8 | type Client struct { 9 | client *http.Client 10 | 11 | LatestResponse *Response 12 | } 13 | 14 | type Request struct { 15 | client *Client 16 | 17 | method, url, host string 18 | 19 | header http.Header 20 | 21 | body io.Reader 22 | 23 | cookies []*http.Cookie 24 | } 25 | 26 | type Response struct { 27 | headers http.Header 28 | 29 | body []byte 30 | 31 | status string 32 | statusCode int 33 | } 34 | --------------------------------------------------------------------------------