blob: f9dfcaf6bb70e2e6f28e15b46c034504ecede45f [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package common
21
22import (
Jens Geyerc6b991f2015-08-07 23:41:09 +020023 "compress/zlib"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "crypto/tls"
25 "flag"
26 "fmt"
claudemirof8ca0552016-01-10 23:31:30 -020027 "net/http"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070028
29 "github.com/apache/thrift/lib/go/thrift"
30 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020031)
32
33var debugClientProtocol bool
34
35func init() {
36 flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on")
37}
38
39func StartClient(
40 host string,
41 port int64,
42 domain_socket string,
43 transport string,
44 protocol string,
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070045 ssl bool,
46) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020047 hostPort := fmt.Sprintf("%s:%d", host, port)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070048 cfg := &thrift.TConfiguration{
49 TLSConfig: &tls.Config{
50 InsecureSkipVerify: true,
51 },
52 }
Jens Geyerf4598682014-05-08 23:18:44 +020053
54 var protocolFactory thrift.TProtocolFactory
55 switch protocol {
56 case "compact":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070057 protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020058 case "simplejson":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070059 protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020060 case "json":
61 protocolFactory = thrift.NewTJSONProtocolFactory()
62 case "binary":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070063 protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg)
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080064 case "header":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070065 protocolFactory = thrift.NewTHeaderProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020066 default:
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070067 return nil, nil, fmt.Errorf("invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020068 }
69 if debugClientProtocol {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070070 protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil))
Jens Geyerf4598682014-05-08 23:18:44 +020071 }
Jens Geyerf4598682014-05-08 23:18:44 +020072 if ssl {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070073 trans = thrift.NewTSSLSocketConf(hostPort, cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020074 } else {
75 if domain_socket != "" {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070076 trans = thrift.NewTSocketConf(domain_socket, nil)
Jens Geyerf4598682014-05-08 23:18:44 +020077 } else {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070078 trans = thrift.NewTSocketConf(hostPort, nil)
Jens Geyerf4598682014-05-08 23:18:44 +020079 }
80 }
81 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020082 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020083 }
84 switch transport {
85 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020086 if ssl {
87 tr := &http.Transport{
88 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
89 }
90 client := &http.Client{Transport: tr}
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070091 trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
claudemirof8ca0552016-01-10 23:31:30 -020092 fmt.Println(hostPort)
93 } else {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070094 trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", hostPort))
claudemirof8ca0552016-01-10 23:31:30 -020095 }
Jens Geyerf4598682014-05-08 23:18:44 +020096 case "framed":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070097 trans = thrift.NewTFramedTransportConf(trans, cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020098 case "buffered":
99 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200100 case "zlib":
101 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200102 case "":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700103 // Do nothing
Jens Geyerf4598682014-05-08 23:18:44 +0200104 default:
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700105 return nil, nil, fmt.Errorf("invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200106 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200107 if err != nil {
108 return nil, nil, err
109 }
Jens Geyerf4598682014-05-08 23:18:44 +0200110 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200111 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200112 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200113 iprot := protocolFactory.GetProtocol(trans)
114 oprot := protocolFactory.GetProtocol(trans)
115 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200116 return
117}