Skip to content

Commit 21fa205

Browse files
authored
Merge pull request #8 from sqlitecloud/#1-Fix-DNS-lookup-on-connect
fix: do not lookup for host before connecting
2 parents 997034b + 71b6112 commit 21fa205

File tree

2 files changed

+60
-74
lines changed

2 files changed

+60
-74
lines changed

connection.go

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ type SQCloud struct {
7575
ErrorMessage string
7676
}
7777

78-
const CompressModeNo = "NO"
79-
const CompressModeLZ4 = "LZ4"
78+
const SQLiteDefaultPort = 8860
79+
80+
const (
81+
CompressModeNo = "NO"
82+
CompressModeLZ4 = "LZ4"
83+
)
8084

8185
const SQLiteCloudCA = "SQLiteCloudCA"
8286

@@ -108,7 +112,7 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
108112
config = &SQCloudConfig{}
109113

110114
config.Host = u.Hostname()
111-
config.Port = 0
115+
config.Port = SQLiteDefaultPort
112116
config.Username = u.User.Username()
113117
config.Password, _ = u.User.Password()
114118
config.Database = strings.TrimPrefix(u.Path, "/")
@@ -243,74 +247,10 @@ func (this *SQCloud) CheckConnectionParameter() error {
243247
return fmt.Errorf("Invalid hostname (%s)", this.Host)
244248
}
245249

246-
// ip := net.ParseIP(this.Host)
247-
// if ip == nil {
248-
// if _, err := net.LookupHost(this.Host); err != nil {
249-
// return errors.New(fmt.Sprintf("Can't resolve hostname (%s)", this.Host))
250-
// }
251-
// }
252-
253-
if this.Port == 0 {
254-
this.Port = 8860
255-
}
256-
if this.Port < 1 || this.Port >= 0xFFFF {
257-
return errors.New(fmt.Sprintf("Invalid Port (%d)", this.Port))
258-
}
259-
260-
// if this.Timeout == 0 {
261-
// this.Timeout = 10 * time.Second
262-
// }
263250
if this.Timeout < 0 {
264251
return errors.New(fmt.Sprintf("Invalid Timeout (%s)", this.Timeout.String()))
265252
}
266253

267-
switch this.CompressMode {
268-
case CompressModeNo, CompressModeLZ4:
269-
default:
270-
return errors.New(fmt.Sprintf("Invalid compression method (%s)", this.CompressMode))
271-
}
272-
273-
if this.Secure {
274-
var pool *x509.CertPool = nil
275-
pem := []byte{}
276-
277-
switch _, _, trimmed := ParseTlsString(this.Pem); trimmed {
278-
case "":
279-
break
280-
case SQLiteCloudCA:
281-
pem = []byte(sqliteCloudCAPEM)
282-
default:
283-
// check if it is a filepath
284-
_, err := os.Stat(trimmed)
285-
if os.IsNotExist(err) {
286-
// not a filepath, use the string as a pem string
287-
pem = []byte(trimmed)
288-
} else {
289-
// its a file, read its content into the pem string
290-
switch bytes, err := os.ReadFile(trimmed); {
291-
case err != nil:
292-
return errors.New(fmt.Sprintf("Could not open PEM file in '%s'", trimmed))
293-
default:
294-
pem = bytes
295-
}
296-
}
297-
}
298-
299-
if len(pem) > 0 {
300-
pool = x509.NewCertPool()
301-
302-
if !pool.AppendCertsFromPEM(pem) {
303-
return errors.New(fmt.Sprintf("Could not append certs from PEM"))
304-
}
305-
}
306-
307-
this.cert = &tls.Config{
308-
RootCAs: pool,
309-
InsecureSkipVerify: this.TlsInsecureSkipVerify,
310-
MinVersion: tls.VersionTLS12,
311-
}
312-
}
313-
314254
return nil
315255
}
316256

@@ -353,12 +293,11 @@ func Connect(ConnectionString string) (*SQCloud, error) {
353293
func (this *SQCloud) Connect() error {
354294
this.reset() // also closes an open connection
355295

356-
switch err := this.CheckConnectionParameter(); {
357-
case err != nil:
296+
if err := this.CheckConnectionParameter(); err != nil {
358297
return err
359-
default:
360-
return this.reconnect()
361298
}
299+
300+
return this.reconnect()
362301
}
363302

364303
// reconnect closes and then reopens a connection to the SQLite Cloud database server.
@@ -369,6 +308,14 @@ func (this *SQCloud) reconnect() error {
369308

370309
this.resetError()
371310

311+
if this.Secure {
312+
cert, err := getTlsConfig(&this.SQCloudConfig)
313+
if err != nil {
314+
return err
315+
}
316+
this.cert = cert
317+
}
318+
372319
var dialer = net.Dialer{}
373320
dialer.Timeout = this.Timeout
374321
dialer.DualStack = true
@@ -437,6 +384,47 @@ func (this *SQCloud) Close() error {
437384
return nil
438385
}
439386

387+
func getTlsConfig(config *SQCloudConfig) (*tls.Config, error) {
388+
var pool *x509.CertPool = nil
389+
pem := []byte{}
390+
391+
switch _, _, trimmed := ParseTlsString(config.Pem); trimmed {
392+
case "":
393+
break
394+
case SQLiteCloudCA:
395+
pem = []byte(sqliteCloudCAPEM)
396+
default:
397+
// check if it is a filepath
398+
_, err := os.Stat(trimmed)
399+
if os.IsNotExist(err) {
400+
// not a filepath, use the string as a pem string
401+
pem = []byte(trimmed)
402+
} else {
403+
// its a file, read its content into the pem string
404+
switch bytes, err := os.ReadFile(trimmed); {
405+
case err != nil:
406+
return nil, fmt.Errorf("could not open PEM file in '%s'", trimmed)
407+
default:
408+
pem = bytes
409+
}
410+
}
411+
}
412+
413+
if len(pem) > 0 {
414+
pool = x509.NewCertPool()
415+
416+
if !pool.AppendCertsFromPEM(pem) {
417+
return nil, fmt.Errorf("could not append certs from PEM")
418+
}
419+
}
420+
421+
return &tls.Config{
422+
RootCAs: pool,
423+
InsecureSkipVerify: config.TlsInsecureSkipVerify,
424+
MinVersion: tls.VersionTLS12,
425+
}, nil
426+
}
427+
440428
func connectionCommands(config SQCloudConfig) (string, []interface{}) {
441429
buffer := ""
442430
args := []interface{}{}

test/compress_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import (
2828
)
2929

3030
const testDbnameCompress = "test-gosdk-compress-db.sqlite"
31-
const testCompressKey = "compress"
32-
const testCompressValue = "LZ4"
3331

3432
func TestCompress(t *testing.T) {
3533
connectionString, _ := os.LookupEnv("SQLITE_CONNECTION_STRING")
@@ -38,7 +36,7 @@ func TestCompress(t *testing.T) {
3836

3937
url, err := url.Parse(connectionString)
4038
values := url.Query()
41-
values.Add(testCompressKey, testCompressValue)
39+
values.Add("compress", sqlitecloud.CompressModeLZ4)
4240
url.RawQuery = values.Encode()
4341

4442
connstring := url.String()

0 commit comments

Comments
 (0)