mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
new provider module HEXONET (#373)
This commit is contained in:
committed by
Craig Peterson
parent
402fc449e2
commit
3e5d223675
242
vendor/github.com/hexonet/go-sdk/client/client.go
generated
vendored
Normal file
242
vendor/github.com/hexonet/go-sdk/client/client.go
generated
vendored
Normal file
@@ -0,0 +1,242 @@
|
||||
// Copyright (c) 2018 Kai Schwarz (1API GmbH). All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the MIT
|
||||
// license that can be found in the LICENSE.md file.
|
||||
|
||||
// Package client contains all you need to communicate with the insanely fast 1API backend API.
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hexonet/go-sdk/client/socketcfg"
|
||||
"github.com/hexonet/go-sdk/response/hashresponse"
|
||||
"github.com/hexonet/go-sdk/response/listresponse"
|
||||
)
|
||||
|
||||
// Client is the entry point class for communicating with the insanely fast 1API backend api.
|
||||
// It allows two ways of communication:
|
||||
// * session based communication
|
||||
// * sessionless communication
|
||||
//
|
||||
// A session based communication makes sense in case you use it to
|
||||
// build your own frontend on top. It allows also to use 2FA
|
||||
// (2 Factor Auth) by providing "otp" in the config parameter of
|
||||
// the login method.
|
||||
// A sessionless communication makes sense in case you do not need
|
||||
// to care about the above and you have just to request some commands.
|
||||
//
|
||||
// Possible commands can be found at https://github.com/hexonet/hexonet-api-documentation/tree/master/API
|
||||
type Client struct {
|
||||
debugMode bool
|
||||
socketTimeout int
|
||||
apiurl string
|
||||
socketcfg.Socketcfg
|
||||
}
|
||||
|
||||
// NewClient represents the constructor for struct Client.
|
||||
// The client is by default set to communicate with the LIVE system. Use method UseOTESystem to switch to the OT&E system instance.
|
||||
func NewClient() *Client {
|
||||
cl := &Client{
|
||||
debugMode: false,
|
||||
socketTimeout: 300000,
|
||||
apiurl: "https://coreapi.1api.net/api/call.cgi",
|
||||
Socketcfg: socketcfg.Socketcfg{},
|
||||
}
|
||||
cl.UseLiveSystem()
|
||||
return cl
|
||||
}
|
||||
|
||||
// EncodeData method to use to encode provided data (socket configuration and api command) before sending it to the API server
|
||||
// It returns the encoded data ready to use within POST request of type "application/x-www-form-urlencoded"
|
||||
func (c *Client) EncodeData(cfg *socketcfg.Socketcfg, cmd map[string]string) string {
|
||||
var tmp, data strings.Builder
|
||||
tmp.WriteString(cfg.EncodeData())
|
||||
tmp.WriteString(url.QueryEscape("s_command"))
|
||||
tmp.WriteString("=")
|
||||
|
||||
for k, v := range cmd {
|
||||
re := regexp.MustCompile(`\r?\n`)
|
||||
v = re.ReplaceAllString(v, "")
|
||||
if len(v) > 0 {
|
||||
data.WriteString(k)
|
||||
data.WriteString("=")
|
||||
data.WriteString(v)
|
||||
data.WriteString("\n")
|
||||
}
|
||||
}
|
||||
tmp.WriteString(url.QueryEscape(data.String()))
|
||||
return tmp.String()
|
||||
}
|
||||
|
||||
// Getapiurl is the getter method for apiurl property
|
||||
func (c *Client) Getapiurl() string {
|
||||
return c.apiurl
|
||||
}
|
||||
|
||||
// Setapiurl is the setter method for apiurl
|
||||
func (c *Client) Setapiurl(url string) {
|
||||
c.apiurl = url
|
||||
}
|
||||
|
||||
// SetCredentials method to set username and password and otp code to use for api communication
|
||||
// set otp code to empty string, if you do not use 2FA
|
||||
func (c *Client) SetCredentials(username string, password string, otpcode string) {
|
||||
c.Socketcfg.SetCredentials(username, password, otpcode)
|
||||
}
|
||||
|
||||
// SetIPAddress method to set api client to submit this ip address in api communication
|
||||
func (c *Client) SetIPAddress(ip string) {
|
||||
c.Socketcfg.SetIPAddress(ip)
|
||||
}
|
||||
|
||||
// SetSubuserView method to activate the use of a subuser account as data view
|
||||
func (c *Client) SetSubuserView(username string) {
|
||||
c.Socketcfg.SetUser(username)
|
||||
}
|
||||
|
||||
// ResetSubuserView method to deactivate the use of a subuser account as data view
|
||||
func (c *Client) ResetSubuserView() {
|
||||
c.Socketcfg.SetUser("")
|
||||
}
|
||||
|
||||
// UseLiveSystem method to set api client to communicate with the LIVE backend API
|
||||
func (c *Client) UseLiveSystem() {
|
||||
c.Socketcfg.SetEntity("54cd")
|
||||
}
|
||||
|
||||
// UseOTESystem method to set api client to communicate with the OT&E backend API
|
||||
func (c *Client) UseOTESystem() {
|
||||
c.Socketcfg.SetEntity("1234")
|
||||
}
|
||||
|
||||
// EnableDebugMode method to enable debugMode for debug output
|
||||
func (c *Client) EnableDebugMode() {
|
||||
c.debugMode = true
|
||||
}
|
||||
|
||||
// DisableDebugMode method to disable debugMode for debug output
|
||||
func (c *Client) DisableDebugMode() {
|
||||
c.debugMode = false
|
||||
}
|
||||
|
||||
// Request method requests the given command to the api server and returns the response as ListResponse.
|
||||
func (c *Client) Request(cmd map[string]string) *listresponse.ListResponse {
|
||||
if c.Socketcfg == (socketcfg.Socketcfg{}) {
|
||||
return listresponse.NewListResponse(hashresponse.NewTemplates().Get("expired"))
|
||||
}
|
||||
return c.dorequest(cmd, &c.Socketcfg)
|
||||
}
|
||||
|
||||
// debugRequest method used to trigger debug output in case debugMode is activated
|
||||
func (c *Client) debugRequest(cmd map[string]string, data string, r *listresponse.ListResponse) {
|
||||
if c.debugMode {
|
||||
j, _ := json.Marshal(cmd)
|
||||
fmt.Printf("%s\n", j)
|
||||
fmt.Println("POST: " + data)
|
||||
fmt.Println(strconv.Itoa(r.Code()) + " " + r.Description() + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
// RequestAll method requests ALL entries matching the request criteria by the given command from api server.
|
||||
// So useful for client-side lists. Finally it returns the response as ListResponse.
|
||||
func (c *Client) RequestAll(cmd map[string]string) *listresponse.ListResponse {
|
||||
if c.Socketcfg == (socketcfg.Socketcfg{}) {
|
||||
return listresponse.NewListResponse(hashresponse.NewTemplates().Get("expired"))
|
||||
}
|
||||
cmd["LIMIT"] = "1"
|
||||
cmd["FIRST"] = "0"
|
||||
r := c.dorequest(cmd, &c.Socketcfg)
|
||||
if r.IsSuccess() {
|
||||
cmd["LIMIT"] = strconv.Itoa(r.Total())
|
||||
cmd["FIRST"] = "0"
|
||||
r = c.dorequest(cmd, &c.Socketcfg)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// request the given command to the api server by using the provided socket configuration and return the response as ListResponse.
|
||||
func (c *Client) dorequest(cmd map[string]string, cfg *socketcfg.Socketcfg) *listresponse.ListResponse {
|
||||
data := c.EncodeData(cfg, cmd)
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", c.apiurl, strings.NewReader(data))
|
||||
if err != nil {
|
||||
tpl := hashresponse.NewTemplates().Get("commonerror")
|
||||
tpl = strings.Replace(tpl, "####ERRMSG####", err.Error(), 1)
|
||||
r := listresponse.NewListResponse(tpl)
|
||||
c.debugRequest(cmd, data, r)
|
||||
return r
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Add("Expect", "")
|
||||
resp, err2 := client.Do(req)
|
||||
if err2 != nil {
|
||||
tpl := hashresponse.NewTemplates().Get("commonerror")
|
||||
tpl = strings.Replace(tpl, "####ERRMSG####", err2.Error(), 1)
|
||||
r := listresponse.NewListResponse(tpl)
|
||||
c.debugRequest(cmd, data, r)
|
||||
return r
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
response, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
tpl := hashresponse.NewTemplates().Get("commonerror")
|
||||
tpl = strings.Replace(tpl, "####ERRMSG####", err.Error(), 1)
|
||||
r := listresponse.NewListResponse(tpl)
|
||||
c.debugRequest(cmd, data, r)
|
||||
return r
|
||||
}
|
||||
r := listresponse.NewListResponse(string(response))
|
||||
c.debugRequest(cmd, data, r)
|
||||
return r
|
||||
}
|
||||
tpl := hashresponse.NewTemplates().Get("commonerror")
|
||||
tpl = strings.Replace(tpl, "####ERRMSG####", string(resp.StatusCode)+resp.Status, 1)
|
||||
r := listresponse.NewListResponse(tpl)
|
||||
c.debugRequest(cmd, data, r)
|
||||
return r
|
||||
}
|
||||
|
||||
// Login method to use as entry point for session based communication.
|
||||
// Response is returned as ListResponse.
|
||||
func (c *Client) Login() *listresponse.ListResponse {
|
||||
return c.dologin(map[string]string{"COMMAND": "StartSession"})
|
||||
}
|
||||
|
||||
// LoginExtended method to use as entry point for session based communication.
|
||||
// This method allows to provide further command parameters for startsession command.
|
||||
// Response is returned as ListResponse.
|
||||
func (c *Client) LoginExtended(cmdparams map[string]string) *listresponse.ListResponse {
|
||||
cmd := map[string]string{"COMMAND": "StartSession"}
|
||||
for k, v := range cmdparams {
|
||||
cmd[k] = v
|
||||
}
|
||||
return c.dologin(cmd)
|
||||
}
|
||||
|
||||
// dologin method used internally to perform a login using the given command.
|
||||
// Response is returned as ListResponse.
|
||||
func (c *Client) dologin(cmd map[string]string) *listresponse.ListResponse {
|
||||
r := c.dorequest(cmd, &c.Socketcfg)
|
||||
if r.Code() == 200 {
|
||||
sessid, _ := r.GetColumnIndex("SESSION", 0)
|
||||
c.Socketcfg.SetSession(sessid)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// Logout method to use for session based communication.
|
||||
// This method logs you out and destroys the api session.
|
||||
// Response is returned as ListResponse.
|
||||
func (c *Client) Logout() *listresponse.ListResponse {
|
||||
cmd := map[string]string{"COMMAND": "EndSession"}
|
||||
return c.dorequest(cmd, &c.Socketcfg)
|
||||
}
|
106
vendor/github.com/hexonet/go-sdk/client/socketcfg/socketcfg.go
generated
vendored
Normal file
106
vendor/github.com/hexonet/go-sdk/client/socketcfg/socketcfg.go
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2018 Kai Schwarz (1API GmbH). All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the MIT
|
||||
// license that can be found in the LICENSE.md file.
|
||||
|
||||
// Package socketcfg provides apiconnector client connection settings
|
||||
package socketcfg
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Socketcfg is a struct representing connection settings used as POST data for http request against the insanely fast 1API backend API.
|
||||
type Socketcfg struct {
|
||||
login string
|
||||
pw string
|
||||
remoteaddr string
|
||||
entity string
|
||||
session string
|
||||
user string
|
||||
otp string
|
||||
}
|
||||
|
||||
// SetIPAddress method to set remote ip address to be submitted to the HEXONET API.
|
||||
// This ip address is being considered when you have ip filter settings activated.
|
||||
// To reset this, simply provide an empty string as parameter.
|
||||
func (s *Socketcfg) SetIPAddress(ip string) {
|
||||
s.remoteaddr = ip
|
||||
}
|
||||
|
||||
// SetCredentials method to set username and password to use for api communication
|
||||
func (s *Socketcfg) SetCredentials(username string, password string, otpcode string) {
|
||||
s.login = username
|
||||
s.pw = password
|
||||
s.otp = otpcode
|
||||
}
|
||||
|
||||
// SetEntity method to set the system entity id used to communicate with
|
||||
// "1234" -> OT&E system, "54cd" -> LIVE system
|
||||
func (s *Socketcfg) SetEntity(entityid string) {
|
||||
s.entity = entityid
|
||||
}
|
||||
|
||||
// SetSession method to set a API session id to use for api communication instead of credentials
|
||||
// which is basically required in case you plan to use session based communication or if you want to use 2FA
|
||||
func (s *Socketcfg) SetSession(sessionid string) {
|
||||
s.login = ""
|
||||
s.pw = ""
|
||||
s.otp = ""
|
||||
s.session = sessionid
|
||||
}
|
||||
|
||||
// SetUser method to set an user account (must be subuser account of your login user) to use for API communication
|
||||
// use this if you want to make changes on that subuser account or if you want to have his data view
|
||||
func (s *Socketcfg) SetUser(username string) {
|
||||
s.user = username
|
||||
}
|
||||
|
||||
// EncodeData method to return the struct data ready to submit within POST request of type "application/x-www-form-urlencoded"
|
||||
func (s *Socketcfg) EncodeData() string {
|
||||
var tmp strings.Builder
|
||||
if len(s.login) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_login"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.login))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.pw) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_pw"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.pw))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.remoteaddr) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_remoteaddr"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.remoteaddr))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.entity) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_entity"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.entity))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.session) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_session"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.session))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.user) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_user"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.user))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
if len(s.otp) > 0 {
|
||||
tmp.WriteString(url.QueryEscape("s_otp"))
|
||||
tmp.WriteString("=")
|
||||
tmp.WriteString(url.QueryEscape(s.otp))
|
||||
tmp.WriteString("&")
|
||||
}
|
||||
return tmp.String()
|
||||
}
|
Reference in New Issue
Block a user