blob: 2383b82e6c20102cd9384d49511c5b5802e599cb [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(
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070040 addr string,
Jens Geyerf4598682014-05-08 23:18:44 +020041 transport string,
42 protocol string,
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070043 ssl bool,
44) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070045 cfg := &thrift.TConfiguration{
46 TLSConfig: &tls.Config{
47 InsecureSkipVerify: true,
48 },
49 }
Jens Geyerf4598682014-05-08 23:18:44 +020050
51 var protocolFactory thrift.TProtocolFactory
52 switch protocol {
53 case "compact":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070054 protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020055 case "simplejson":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070056 protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020057 case "json":
58 protocolFactory = thrift.NewTJSONProtocolFactory()
59 case "binary":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070060 protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg)
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080061 case "header":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070062 protocolFactory = thrift.NewTHeaderProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020063 default:
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070064 return nil, nil, fmt.Errorf("invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020065 }
66 if debugClientProtocol {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070067 protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil))
Jens Geyerf4598682014-05-08 23:18:44 +020068 }
Jens Geyerf4598682014-05-08 23:18:44 +020069 if ssl {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070070 trans = thrift.NewTSSLSocketConf(addr, cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020071 } else {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070072 trans = thrift.NewTSocketConf(addr, nil)
Jens Geyerf4598682014-05-08 23:18:44 +020073 }
74 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020075 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020076 }
77 switch transport {
78 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020079 if ssl {
80 tr := &http.Transport{
81 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
82 }
83 client := &http.Client{Transport: tr}
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070084 trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", addr), thrift.THttpClientOptions{Client: client})
claudemirof8ca0552016-01-10 23:31:30 -020085 } else {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070086 trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", addr))
claudemirof8ca0552016-01-10 23:31:30 -020087 }
Jens Geyerf4598682014-05-08 23:18:44 +020088 case "framed":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070089 trans = thrift.NewTFramedTransportConf(trans, cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020090 case "buffered":
91 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020092 case "zlib":
93 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +020094 case "":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070095 // Do nothing
Jens Geyerf4598682014-05-08 23:18:44 +020096 default:
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070097 return nil, nil, fmt.Errorf("invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +020098 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +020099 if err != nil {
100 return nil, nil, err
101 }
Jens Geyerf4598682014-05-08 23:18:44 +0200102 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200103 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200104 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200105 iprot := protocolFactory.GetProtocol(trans)
106 oprot := protocolFactory.GetProtocol(trans)
107 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200108 return
109}