Commit 638ffe8d by usual2970

init

parents
package main
import (
"github.com/usual2970/util/tel"
)
func main() {
tel := tel.NewTel("18257140570", "15000210629")
tel.Dial()
}
package random
import (
"crypto/rand"
r "math/rand"
"time"
)
// RandomCreateBytes generate random []byte by specify chars.
func RandomCreateBytes(n int, alphabets ...byte) []byte {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
var randby bool
if num, err := rand.Read(bytes); num != n || err != nil {
r.Seed(time.Now().UnixNano())
randby = true
}
for i, b := range bytes {
if len(alphabets) == 0 {
if randby {
bytes[i] = alphanum[r.Intn(len(alphanum))]
} else {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
} else {
if randby {
bytes[i] = alphabets[r.Intn(len(alphabets))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
}
return bytes
}
package request
import (
"bytes"
"sync"
)
var textBufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 4<<10)) // 4KB
},
}
package request
import (
// "bytes"
"fmt"
"net/http"
"github.com/pquerna/ffjson/ffjson"
"strings"
"github.com/usual2970/util/random"
"github.com/usual2970/util/time"
"crypto/sha256"
"encoding/base64"
)
type Client struct {
appKey string
appSecret string
httpClient *http.Client
}
func (clt *Client) AppKey() string {
return clt.appKey
}
func (clt *Client) AppSecret() string {
return clt.appSecret
}
func NewClient(appKey,appSecret string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{
appKey: appKey,
appSecret: appSecret,
httpClient: httpClient,
}
}
func (clt *Client) Post(url string,params interface{}) (resp interface{},err error){
pBytes,err:=ffjson.Marshal(params)
fmt.Println(string(pBytes))
if err!=nil{
return
}
req,err:=http.NewRequest("POST",url,strings.NewReader(string(pBytes)))
if err!=nil{
return
}
ctime:=time.NowCm()
nonce:=string(random.RandomCreateBytes(64))
pwDigest:=clt.PwDigest(ctime,nonce)
req.Header.Add("Authorization","WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"AppKey\"")
req.Header.Add("X-WSSE",
fmt.Sprintf(`UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"`,
clt.AppKey(),
pwDigest,
nonce,
ctime,
))
req.Header.Add("Content-Type","application/json; charset=UTF-8")
response,err:=clt.httpClient.Do(req)
if err!=nil{
fmt.Println(err)
return
}
defer response.Body.Close()
fmt.Println(req.Header)
return
}
func (clt *Client) PwDigest(cmNow,nonce string) string{
hash:=sha256.New()
hash.Write([]byte(nonce+cmNow+clt.AppSecret()))
md:=hash.Sum(nil)
return base64.StdEncoding.EncodeToString(md)
}
package request
import (
"crypto/tls"
"net"
"net/http"
"crypto/x509"
"time"
"io/ioutil"
)
// NewTLSHttpClient 创建支持双向证书认证的 http.Client
func NewTLSHttpClient(certFile, keyFile string) (httpClient *http.Client, err error) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
}
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
},
Timeout: 60 * time.Second,
}
return
}
func NewTLSHttpClientSingle(certFile string) (httpClient *http.Client, err error){
//x509.Certificate.
pool := x509.NewCertPool()
caCrt, err := ioutil.ReadFile(certFile)
if err != nil {
return
}
pool.AppendCertsFromPEM(caCrt)
//pool.AddCert(caCrt)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
InsecureSkipVerify: true,
},
DisableCompression: true,
}
httpClient = &http.Client{Transport: tr}
return
}
package tel
var AppKey = "34429289df7c45ac90cbd91debcfa5c1"
var AppSecret = "7b90b109fc059f03"
var DispayNumber = "+8657156150625"
var CallbackUrl = "aHR0cDovL2gueWlkaWFubGluZy5jb20vbGlzdGVuL2NiLWNt"
var CerPath="/var/www/yidianling/common/components/yunxun/iSimularClient.pem"
var DialUrl="https://aep.test.sdp.com:1101/tropo/click2CallEx/v1"
var DiaHost="aep.test.sdp.com"
func GetAppKey() string{
return AppKey
}
func GetAppSecret() string{
return AppSecret
}
package tel
import(
"github.com/usual2970/util/request"
)
type Params struct {
DeveloperCDR string `json:"developerCDR"`
DisplayNbr string `json:"displayNbr"`
CallerNbr string `json:"callerNbr"`
CalleeNbr string `json:"calleeNbr"`
LanguageType string `json:"languageType"`
StatusURL string `json:"statusURL"`
}
type Tel struct {
SessionId string
Params *Params
}
func NewTel(from, to string) *Tel {
params := &Params{
DeveloperCDR: "yidianlingTel",
DisplayNbr: DispayNumber,
CallerNbr: "+86" + from,
CalleeNbr: "+86" + to,
LanguageType: "0",
StatusURL: CallbackUrl,
}
return &Tel{
Params: params,
}
}
func (t *Tel) Dial() (v interface{},err error) {
tlsClient,err:=request.NewTLSHttpClientSingle(CerPath)
if err!=nil{
return
}
client:=request.NewClient(AppKey,AppSecret,tlsClient)
client.Post(DialUrl,t.Params)
return
}
func (t *Tel) Cancel() interface{} {
return ""
}
package time
import (
"time"
)
func NowCm() string{
return time.Now().Format("2006-01-02T15:04:05Z")
}
\ No newline at end of file
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment