├── .gitignore ├── LICENSE ├── README.md ├── TODO.md ├── VERSION ├── cpanel ├── account.go ├── auth.go ├── cpanel.go └── models.go └── tests └── cpanel_test.go /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DigivityBV/cpanelgo/72cbd678e11ac52a73329db037c3752ae5cc4447/.gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) <2018> 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 13 | all 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 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Cpanel & WHM API for GOLANG 2 | 3 | ## Contents 4 | - [Installation Guide](#installation-guide) 5 | - [Usage](#usage) 6 | - [Feedback & Contribution](#feedback-&-contribution) 7 | 8 | ### Installation Guide 9 | 10 | To install this package, you can run this code 11 | ``` 12 | go get https://github.com/DigivityBV/cpanelgo 13 | ``` 14 | 15 | 16 | ### Usage 17 | For example, if you would like to get list accounts of your whm server, 18 | 19 | 20 | Firstly auth the whm panel 21 | 22 | ``` 23 | client, err := cp.New(username, host, password) 24 | if err != nil { 25 | panic(err) 26 | } 27 | ``` 28 | 29 | Then list accounts 30 | 31 | ``` 32 | accounts, err := client.ListAccounts() 33 | if err != nil { 34 | panic(err) 35 | } 36 | 37 | ``` 38 | 39 | ### Cpanel API Usage 40 | 41 | Visit [CPANEL API](https://documentation.cpanel.net/display/DD/Guide+to+cPanel+API+2) To all modules and functions 42 | 43 | ``` 44 | client, err := cp.New(username, host, password) 45 | if err != nil { 46 | panic(err) 47 | } 48 | message, err := client.Cpanel(module, function, username) 49 | if err != nil { 50 | panic(err) 51 | } 52 | ``` 53 | #### Feedback and contribution 54 | This package is free and open source, feel free to fork and report some issue to this package. :) 55 | 56 | 57 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | * [x] Auth 2 | * [x] list Accounts 3 | * [x] create Account 4 | * [x] destroy Account 5 | * [x] list users 6 | * [x] get domain info 7 | 8 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.0.0 -------------------------------------------------------------------------------- /cpanel/account.go: -------------------------------------------------------------------------------- 1 | package cpanelgo 2 | 3 | import ( 4 | "github.com/tidwall/gjson" 5 | ) 6 | 7 | func (cp *CP) ListAccounts() ([]gjson.Result, error) { 8 | 9 | data, err := cp.runQuery("listaccts", nil) 10 | if err != nil { 11 | return nil, err 12 | } 13 | 14 | value := gjson.Get(string(data), "acct") 15 | return value.Array(), nil 16 | } 17 | 18 | func (cp *CP) ListUsers() ([]gjson.Result, error) { 19 | 20 | data, err := cp.runQuery("list_users", nil) 21 | if err != nil { 22 | return nil, err 23 | } 24 | 25 | value := gjson.Get(string(data), "data.users") 26 | return value.Array(), nil 27 | } 28 | 29 | func (cp *CP) GetDomainInfo() ([]gjson.Result, error) { 30 | 31 | data, err := cp.runQuery("get_domain_info", nil) 32 | if err != nil { 33 | return nil, err 34 | } 35 | 36 | value := gjson.Get(string(data), "data.domains") 37 | return value.Array(), nil 38 | } 39 | 40 | func (cp *CP) CreateAccount(domain string, username string, password string) ([]gjson.Result, error) { 41 | 42 | data, err := cp.runQuery("createacct", map[string]string{ 43 | "username": username, 44 | "domain": domain, 45 | "password": password, 46 | }) 47 | if err != nil { 48 | return nil, err 49 | } 50 | 51 | value := gjson.Get(string(data), "data") 52 | return value.Array(), nil 53 | } 54 | 55 | func (cp *CP) RemoveAccount(username string) ([]gjson.Result, error) { 56 | data, err := cp.runQuery("removeacct", map[string]string{ 57 | "username": username, 58 | }) 59 | if err != nil { 60 | return nil, err 61 | } 62 | 63 | value := gjson.Get(string(data), "data") 64 | return value.Array(), nil 65 | } 66 | -------------------------------------------------------------------------------- /cpanel/auth.go: -------------------------------------------------------------------------------- 1 | package cpanelgo 2 | 3 | import ( 4 | "crypto/tls" 5 | "errors" 6 | "fmt" 7 | "io/ioutil" 8 | 9 | "github.com/smallnest/goreq" 10 | "github.com/tidwall/gjson" 11 | ) 12 | 13 | func (cp *CP) runQuery(function string, paramaters ...map[string]string) ([]byte, error) { 14 | 15 | params := "" 16 | if paramaters != nil { 17 | for k, v := range paramaters[0] { 18 | params += fmt.Sprintf("%s=%s&", k, v) 19 | } 20 | } 21 | url := fmt.Sprintf("%s://%s:%s/%s/%s?%s", cp.Protocol, cp.WHMHostname, cp.WHMPort, cp.Format, function, params) 22 | fmt.Println(url) 23 | request := goreq.New().SetBasicAuth(cp.WHMUsername, cp.WHMAuthKey) 24 | //Unsecure ssl bypass 25 | request = request.TLSClientConfig(&tls.Config{InsecureSkipVerify: true}) 26 | 27 | resp, _, _ := request.Get(url).End() 28 | body, _ := ioutil.ReadAll(resp.Body) 29 | 30 | value := gjson.Get(string(body), "status") 31 | if value.String() == "0" { 32 | return nil, errors.New("Status Code:0, Please check arguments.") 33 | } 34 | return body, nil 35 | } 36 | -------------------------------------------------------------------------------- /cpanel/cpanel.go: -------------------------------------------------------------------------------- 1 | package cpanelgo 2 | 3 | import ( 4 | "github.com/tidwall/gjson" 5 | ) 6 | 7 | type Maps map[string]string 8 | 9 | func MapsJoin(m1, m2 Maps) Maps { 10 | for ia, va := range m1 { 11 | if it, ok := m2[ia]; ok { 12 | va += it 13 | } 14 | m2[ia] = va 15 | 16 | } 17 | return m2 18 | } 19 | 20 | func (cp *CP) Cpanel(module string, function string, username string, params ...map[string]string) ([]gjson.Result, error) { 21 | values := map[string]string{ 22 | "cpanel_jsonapi_version": "2", 23 | "cpanel_jsonapi_module": module, 24 | "cpanel_jsonapi_func": function, 25 | "cpanel_jsonapi_user": username, 26 | } 27 | if params != nil { 28 | values = MapsJoin(params[0], values) 29 | } 30 | 31 | data, err := cp.runQuery("cpanel", values) 32 | if err != nil { 33 | return nil, err 34 | } 35 | value := gjson.Get(string(data), "cpanelresult.data") 36 | return value.Array(), nil 37 | } 38 | -------------------------------------------------------------------------------- /cpanel/models.go: -------------------------------------------------------------------------------- 1 | package cpanelgo 2 | 3 | import ( 4 | "github.com/asaskevich/govalidator" 5 | "github.com/soheilhy/args" 6 | ) 7 | 8 | type CP struct { 9 | Protocol string `valid:"required~Protocol not found!"` 10 | WHMHostname string `valid:"required~Hostname not found!"` 11 | WHMPort string `valid:"required~Port not found!,int"` 12 | WHMUsername string `valid:"required~Username not found!"` 13 | WHMAuthKey string `valid:"required~Auth Key not found!"` 14 | Format string `valid:"required~Format not found!"` 15 | APIVersion string `valid:"-"` 16 | } 17 | 18 | var ssl = args.NewBool(args.Default(true)) 19 | var apiformat = args.NewString(args.Default("json-api")) 20 | 21 | //Constructr for cpanel client 22 | func New(username string, host string, passkey string, opts ...args.V) (CP, error) { 23 | //Protocol and port assignment 24 | protocol := "https" 25 | port := "2087" 26 | 27 | if !ssl.Get(opts) { 28 | protocol = "http" 29 | port = "2086" 30 | } 31 | 32 | //Api format selection 33 | format := apiformat.Get(opts) 34 | 35 | //Validate 36 | client := CP{ 37 | Protocol: protocol, 38 | WHMHostname: host, 39 | WHMPort: port, 40 | WHMUsername: username, 41 | WHMAuthKey: passkey, 42 | Format: format, 43 | APIVersion: "2", 44 | } 45 | if _, err := govalidator.ValidateStruct(client); err != nil { 46 | return CP{}, err 47 | } 48 | 49 | return client, nil 50 | } 51 | -------------------------------------------------------------------------------- /tests/cpanel_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | cp "cpanelgo/cpanel" 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | //Whm username 10 | var username = "" 11 | 12 | //Whm password 13 | var password = "" 14 | 15 | //Whm hostname address 16 | var host = "" 17 | 18 | func Auth(t *testing.T) *cp.CP { 19 | client, err := cp.New(username, host, password) 20 | if err != nil { 21 | t.Error(err) 22 | } 23 | return &client 24 | } 25 | func TestListAccount(t *testing.T) { 26 | client := Auth(t) 27 | _, err := client.ListAccounts() 28 | if err != nil { 29 | t.Error(err) 30 | } 31 | } 32 | 33 | func TestCreateAccount(t *testing.T) { 34 | domain := "" 35 | username := "" 36 | passkey := "" 37 | client := Auth(t) 38 | _, err := client.CreateAccount(domain, username, passkey) 39 | if err != nil { 40 | t.Error(err) 41 | } 42 | } 43 | 44 | func TestRemoveAccount(t *testing.T) { 45 | username := "" 46 | client := Auth(t) 47 | _, err := client.RemoveAccount(username) 48 | if err != nil { 49 | t.Error(err) 50 | } 51 | } 52 | func TestListUsers(t *testing.T) { 53 | client := Auth(t) 54 | _, err := client.ListUsers() 55 | if err != nil { 56 | t.Error(err) 57 | } 58 | } 59 | 60 | func TestGetDomainInfo(t *testing.T) { 61 | client := Auth(t) 62 | _, err := client.GetDomainInfo() 63 | if err != nil { 64 | t.Error(err) 65 | } 66 | } 67 | 68 | func TestCpanel(t *testing.T) { 69 | module := "DiskUsage" 70 | function := "buildcache" 71 | username := "" 72 | client := Auth(t) 73 | msg, err := client.Cpanel(module, function, username) 74 | if err != nil { 75 | t.Error(err) 76 | } 77 | fmt.Println(msg) 78 | } 79 | --------------------------------------------------------------------------------