commit 522ae84fe6fb67965cfb392e531460d8b54844ff Author: kayos@tcp.direct Date: Thu Dec 9 03:30:43 2021 -0800 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..078212a --- /dev/null +++ b/README.md @@ -0,0 +1,222 @@ +# proxygonanza +-- + import "." + + +## Usage + +```go +const ( + APIBaseURL = "https://proxybonanza.com/api/v1/" +) +``` + +#### func GetMyIP + +```go +func GetMyIP() net.IP +``` + +#### type APIClient + +```go +type APIClient struct { + Key string + KnownPackages map[int]PackageDetails +} +``` + +APIClient is a client for ProxyBonanza.com. + +#### func NewApiClient + +```go +func NewApiClient(key string) *APIClient +``` +NewApiClient instantiates a proxybonanza.com API client with the given key. + +#### func (*APIClient) AddAuthIP + +```go +func (api *APIClient) AddAuthIP(ip net.IP, packageID int) (AddAuthIPResponse, error) +``` +AddAuthIP adds a new IP to the corresponding/provided proxy package ID. + +#### func (*APIClient) AddCurrentIPtoAllPackages + +```go +func (api *APIClient) AddCurrentIPtoAllPackages() (success int) +``` +AddCurrentIPtoAllPackages adds your current WAN IP to all packages on your +account. It returns the amount of successful packages that it was applied to. + +#### func (*APIClient) DeleteAllAuthIPs + +```go +func (api *APIClient) DeleteAllAuthIPs() (int, error) +``` +DeleteAllAuthIPs deletes all authenticaiton IPs from your account. + +#### func (*APIClient) DeleteAuthIPByID + +```go +func (api *APIClient) DeleteAuthIPByID(ipID int) (ok bool) +``` +DeleteAuthIPByID deletes an authentication IP with the matching ID provided + +#### func (*APIClient) DeleteAuthIPByIP + +```go +func (api *APIClient) DeleteAuthIPByIP(ipa net.IP) (err error) +``` +DeleteAuthIPByIP will iterate through all the authips on your account and delete +one that matches the given IP. + +#### func (*APIClient) GetAuthIPs + +```go +func (api *APIClient) GetAuthIPs() ([]AuthIP, error) +``` +GetAuthIPs gets all authentication IPs active on your account. + +#### func (*APIClient) GetProxyPackages + +```go +func (api *APIClient) GetProxyPackages() ([]UserPackage, error) +``` +GetProxyPackages gets current proxy packages from your account. + +#### type AddAuthIPResponse + +```go +type AddAuthIPResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + Data struct { + ID int `json:"id"` + } `json:"data"` +} +``` + +AddAuthIPResponse represents an API response from proxybonanza.com. + +#### type AuthIP + +```go +type AuthIP struct { + UserpackageID int `json:"userpackage_id"` + ID interface{} `json:"id"` + IP string `json:"ip"` +} +``` + +AuthIP is an IP address authorized to use the proxies in the related package. + +#### type AuthIPResponse + +```go +type AuthIPResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + AuthIPData []AuthIP `json:"data"` + Pages pagination `json:"pagination"` +} +``` + +AuthIPResponse represents an API response from proxybonanza.com. + +#### type DelAuthIPResponse + +```go +type DelAuthIPResponse struct { + Success bool `json:"success"` +} +``` + +DelAuthIPResponse represents an API response from proxybonanza.com. + +#### type Package + +```go +type Package struct { + ID int + AuthIPs []AuthIP + AllTimeStats PackageStatistics + HourlyStats map[time.Time]PackageStatistics +} +``` + +Package contains what we know about a particular proxybonanza package. + +#### type PackageDetails + +```go +type PackageDetails struct { + Name string `json:"name"` + Bandwidth int64 `json:"bandwidth"` + Price interface{} `json:"price"` + HowmanyIPs int `json:"howmany_ips"` + PricePerGig interface{} `json:"price_per_gig"` + PackageType string `json:"package_type"` + HowmanyAuthips int `json:"howmany_authips"` + IPType int `json:"ip_type"` + PriceUserFormatted string `json:"price_user_formatted"` +} +``` + +PackageDetails represents an API response from proxybonanza.com containing proxy +package information. + +#### type PackageResponse + +```go +type PackageResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + PackageData []UserPackage `json:"data"` + Pages pagination `json:"pagination"` +} +``` + +PackageResponse represents an API response from proxybonanza.com containing +proxy package information. + +#### type PackageStatistics + +```go +type PackageStatistics struct { + UserpackageID int `json:"userpackage_id"` + Date string `json:"date"` + BndHTTP int `json:"bnd_http"` + ConnHTTP int `json:"conn_http"` + BndSocks int `json:"bnd_socks"` + ConnSocks int `json:"conn_socks"` + BndTotal int `json:"bnd_total"` + ConnTotal int `json:"conn_total"` +} +``` + +PackageStatistics represents the statistics for the related proxy package. + +#### type UserPackage + +```go +type UserPackage struct { + ID int `json:"id"` + CustomName interface{} `json:"custom_name"` + Login string `json:"login"` + Password string `json:"password"` + Expires time.Time `json:"expires"` + Bandwidth int64 `json:"bandwidth"` + LastIPChange time.Time `json:"last_ip_change"` + LowBanwidthNotificationPercent int `json:"low_banwidth_notification_percent"` + Package PackageDetails `json:"package"` + BandwidthGb float64 `json:"bandwidth_gb"` + AdditionalBandwidthGb int `json:"additional_bandwidth_gb"` + BandwidthPercentLeftHuman string `json:"bandwidth_percent_left_human"` + ExpirationDateHuman string `json:"expiration_date_human"` + Name string `json:"name"` +} +``` + +UserPackage represents a proxy package purchased from proxybonanza.com. diff --git a/_example/cmd/getpackages/main.go b/_example/cmd/getpackages/main.go new file mode 100644 index 0000000..7fdf70a --- /dev/null +++ b/_example/cmd/getpackages/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + "git.tcp.direct/kayos/proxygonanza" +) + +func main() { + c := proxygonanza.NewApiClient(os.Args[1]) + println("getting proxy packages...") + + packs, err := c.GetProxyPackages() + if err != nil { + println(err.Error()) + // return + } + fmt.Printf("\nfound %d proxy packages\n", len(packs)) + for _, p := range packs { + pretty, _ := json.MarshalIndent(p, "", "\t") + fmt.Print(string(pretty)) + } + + println("getting auth IPs...") + authips, err := c.GetAuthIPs() + if err != nil { + println(err.Error()) + // return + } + + fmt.Printf("\nfound %d auth IPs\n", len(authips)) + for _, i := range authips { + pretty, _ := json.MarshalIndent(i, "", "\t") + fmt.Print(string(pretty)+"\n") + } +} diff --git a/_example/cmd/resetAuthIPs/main.go b/_example/cmd/resetAuthIPs/main.go new file mode 100644 index 0000000..5c59b7f --- /dev/null +++ b/_example/cmd/resetAuthIPs/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" + "strconv" + + "git.tcp.direct/kayos/proxygonanza" +) + +func main() { + println("clearing all auth IPs...") + + c := proxygonanza.NewApiClient(os.Args[1]) + deleted, err := c.DeleteAllAuthIPs() + if err != nil { + println(err.Error()) + return + } + + println("deleted " + strconv.Itoa(deleted) + " IPs successfully") + + + println("adding current IP to all packages...") + count := c.AddCurrentIPtoAllPackages() + if count == 0 { + println("failed!") + return + } + + fmt.Printf("successfully added your external IP to %d packages\n", count) +} diff --git a/actions.go b/actions.go new file mode 100644 index 0000000..3221ff5 --- /dev/null +++ b/actions.go @@ -0,0 +1,267 @@ +package proxygonanza + +import ( + "encoding/json" + "errors" + "fmt" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "time" +) + +const ( + APIBaseURL = "https://proxybonanza.com/api/v1/" + packages = "userpackages.json" + authips = "authips.json" +) + +// Package contains what we know about a particular proxybonanza package. +type Package struct { + ID int + AuthIPs []AuthIP + AllTimeStats PackageStatistics + HourlyStats map[time.Time]PackageStatistics +} + +// APIClient is a client for ProxyBonanza.com. +type APIClient struct { + Key string + KnownPackages map[int]PackageDetails + + c *http.Client +} + +// NewApiClient instantiates a proxybonanza.com API client with the given key. +func NewApiClient(key string) *APIClient { + return &APIClient{ + Key: key, + KnownPackages: make(map[int]PackageDetails), + c: http.DefaultClient, + } +} + +func (api *APIClient) newRequest(method, u string) (r *http.Request) { + r, _ = http.NewRequest(method, u, nil) + r.Header.Add("accept", "application/json") + r.Header.Add("Authorization", api.Key) + + return +} + +func (api *APIClient) getReq(endpoint string) ([]byte, error) { + res, err := api.c.Do(api.newRequest("GET", APIBaseURL+endpoint)) + body, err := processBody(res) + if err != nil { + return nil, err + } + return body, nil +} + +func (api *APIClient) postReq(endpoint string, post map[string]string) ([]byte, error) { + params := url.Values{} + for k, v := range post { + params.Set(k, v) + } + enc := params.Encode() + req, err := http.NewRequest("POST", APIBaseURL+endpoint, strings.NewReader(enc)) + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", api.Key) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + res, err := api.c.Do(req) + if err != nil { + return nil, err + } + body, err := processBody(res) + if err != nil { + return nil, err + } + return body, nil +} + +func (api *APIClient) deleteReq(endpoint string) ([]byte, error) { + req, err := http.NewRequest("DELETE", APIBaseURL+endpoint, nil) + + if err != nil { + return nil, err + } + + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", api.Key) + + res, err := api.c.Do(req) + if err != nil { + return nil, err + } + + body, err := processBody(res) + if err != nil { + return nil, err + } + + return body, nil +} + +// GetProxyPackages gets current proxy packages from your account. +func (api *APIClient) GetProxyPackages() ([]UserPackage, error) { + body, err := api.getReq(packages) + if err != nil { + return nil, err + } + + var packs PackageResponse + + err = json.Unmarshal(body, &packs) + if err != nil { + return nil, err + } + + if !packs.Success { + return nil, errors.New("ERROR: " + string(body)) + } + + for _, p := range packs.PackageData { + api.KnownPackages[p.ID] = p.Package + } + + return packs.PackageData, nil +} + +// GetAuthIPs gets all authentication IPs active on your account. +func (api *APIClient) GetAuthIPs() ([]AuthIP, error) { + body, err := api.getReq(authips) + if err != nil { + return nil, err + } + + var auths AuthIPResponse + + err = json.Unmarshal(body, &auths) + if err != nil { + return nil, err + } + + if !auths.Success { + return nil, errors.New("ERROR: " + string(body)) + } + + return auths.AuthIPData, nil +} + +// DeleteAllAuthIPs deletes all authenticaiton IPs from your account. +func (api *APIClient) DeleteAllAuthIPs() (int, error) { + aips, err := api.GetAuthIPs() + if err != nil { + return 0, err + } + + todo := len(aips) + done := 0 + + for _, aip := range aips { + target := int(aip.ID.(float64)) + fmt.Println("deleting ", target) + if api.DeleteAuthIPByID(target) { + done++ + } + } + if done < todo { + err = errors.New("failed to delete some IPs") + } + return done, err +} + +// AddAuthIP adds a new IP to the corresponding/provided proxy package ID. +func (api *APIClient) AddAuthIP(ip net.IP, packageID int) (AddAuthIPResponse, error) { + failadd := AddAuthIPResponse{Success: false} + if ip.IsPrivate() || ip.IsUnspecified() || ip.IsLoopback() { + return failadd, errors.New("ip is private: " + ip.String()) + } + + post := map[string]string{ + "ip": ip.String(), + "userpackage_id": strconv.Itoa(packageID), + } + + body, err := api.postReq(authips, post) + if err != nil { + return failadd, err + } + + var addipres AddAuthIPResponse + + err = json.Unmarshal(body, &addipres) + if err != nil { + return failadd, err + } + + if !addipres.Success { + return failadd, errors.New("ERROR: " + addipres.Message) + } + + return addipres, nil +} + +// DeleteAuthIPByIP will iterate through all the authips on your account and delete one that matches the given IP. +func (api *APIClient) DeleteAuthIPByIP(ipa net.IP) (err error) { + if ipa.IsPrivate() || ipa.IsUnspecified() || ipa.IsLoopback() { + return errors.New("IP is invalid") + } + aips, err := api.GetAuthIPs() + for _, aip := range aips { + if net.ParseIP(aip.IP).Equal(ipa) { + target := int(aip.ID.(float64)) + if api.DeleteAuthIPByID(target) { + return nil + } + } + } + return errors.New("IP doesn't exist") +} + +// AddCurrentIPtoAllPackages adds your current WAN IP to all packages on your account. +// It returns the amount of successful packages that it was applied to. +func (api *APIClient) AddCurrentIPtoAllPackages() (success int) { + packs, err := api.GetProxyPackages() + if err != nil { + fmt.Println(err) + return + } + + myip := GetMyIP() + for _, p := range packs { + _, err := api.AddAuthIP(myip, p.ID) + if err == nil { + success++ + } + } + return +} + +// DeleteAuthIPByID deletes an authentication IP with the matching ID provided +func (api *APIClient) DeleteAuthIPByID(ipID int) (ok bool) { + body, err := api.deleteReq("authips/" + strconv.Itoa(ipID) + ".json") + if err != nil { + return + } + + var delipres DelAuthIPResponse + err = json.Unmarshal(body, &delipres) + + if err != nil { + return + } + + if delipres.Success { + ok = true + } + + return +} diff --git a/api.go b/api.go new file mode 100644 index 0000000..98c3c52 --- /dev/null +++ b/api.go @@ -0,0 +1,92 @@ +package proxygonanza + +import "time" + +// PackageResponse represents an API response from proxybonanza.com containing proxy package information. +type PackageResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + PackageData []UserPackage `json:"data"` + Pages pagination `json:"pagination"` +} + +// AuthIPResponse represents an API response from proxybonanza.com. +type AuthIPResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + AuthIPData []AuthIP `json:"data"` + Pages pagination `json:"pagination"` +} + +// AddAuthIPResponse represents an API response from proxybonanza.com. +type AddAuthIPResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + Data struct { + ID int `json:"id"` + } `json:"data"` +} + +// DelAuthIPResponse represents an API response from proxybonanza.com. +type DelAuthIPResponse struct { + Success bool `json:"success"` +} + +// PackageDetails represents an API response from proxybonanza.com containing proxy package information. +type PackageDetails struct { + Name string `json:"name"` + Bandwidth int64 `json:"bandwidth"` + Price interface{} `json:"price"` + HowmanyIPs int `json:"howmany_ips"` + PricePerGig interface{} `json:"price_per_gig"` + PackageType string `json:"package_type"` + HowmanyAuthips int `json:"howmany_authips"` + IPType int `json:"ip_type"` + PriceUserFormatted string `json:"price_user_formatted"` +} + +type pagination struct { + PageCount int `json:"page_count"` + CurrentPage int `json:"current_page"` + HasNextPage bool `json:"has_next_page"` + HasPrevPage bool `json:"has_prev_page"` + Count int `json:"count"` + Limit interface{} `json:"limit"` +} + +// UserPackage represents a proxy package purchased from proxybonanza.com. +type UserPackage struct { + ID int `json:"id"` + CustomName interface{} `json:"custom_name"` + Login string `json:"login"` + Password string `json:"password"` + Expires time.Time `json:"expires"` + Bandwidth int64 `json:"bandwidth"` + LastIPChange time.Time `json:"last_ip_change"` + LowBanwidthNotificationPercent int `json:"low_banwidth_notification_percent"` + Package PackageDetails `json:"package"` + BandwidthGb float64 `json:"bandwidth_gb"` + AdditionalBandwidthGb int `json:"additional_bandwidth_gb"` + BandwidthPercentLeftHuman string `json:"bandwidth_percent_left_human"` + ExpirationDateHuman string `json:"expiration_date_human"` + Name string `json:"name"` +} + +// AuthIP is an IP address authorized to use the proxies in the related package. +type AuthIP struct { + UserpackageID int `json:"userpackage_id"` + ID interface{} `json:"id"` + IP string `json:"ip"` +} + +// PackageStatistics represents the statistics for the related proxy package. +type PackageStatistics struct { + UserpackageID int `json:"userpackage_id"` + Date string `json:"date"` + BndHTTP int `json:"bnd_http"` + ConnHTTP int `json:"conn_http"` + BndSocks int `json:"bnd_socks"` + ConnSocks int `json:"conn_socks"` + BndTotal int `json:"bnd_total"` + ConnTotal int `json:"conn_total"` +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4c37318 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.tcp.direct/kayos/proxygonanza + +go 1.17 diff --git a/util.go b/util.go new file mode 100644 index 0000000..3c141cb --- /dev/null +++ b/util.go @@ -0,0 +1,33 @@ +package proxygonanza + +import ( + "fmt" + "io/ioutil" + "net" + "net/http" + "strings" +) + +func processBody(res *http.Response) ([]byte, error) { + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + return body, nil +} + +func GetMyIP() net.IP { + res, err := http.DefaultClient.Get("https://wtfismyip.com/text") + if err != nil { + fmt.Println(err) + return net.IP{} + } + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + return net.IP{} + } + return net.ParseIP(strings.TrimSpace(string(body))) +}