Skip to content

Commit

Permalink
Merge pull request #1 from paicha/fix
Browse files Browse the repository at this point in the history
CHG: refactor a little bit
  • Loading branch information
paicha authored Jan 24, 2022
2 parents 0836049 + 649f5e8 commit e029571
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 51 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
# DASLink
todo
DASLink is a simple tool to link ipfs content from [DAS](https://da.systems/).

## How does it work?
Dependent on [DNSLink](https://docs.ipfs.io/concepts/dnslink/), [Cloudflare ipfs gateway](https://developers.cloudflare.com/distributed-web/ipfs-gateway), [Cloudflare DNS](https://api.cloudflare.com/#dns-records-for-a-zone-properties) and [das-database](https://github.com/DeAccountSystems/das-database).

```
┌───────────┐ ┌───────────┐ ┌────────────┐
│ │ │ │ │ │
│ Alice │ │ DNS │ │ipfs gateway│
│ │ │ │ │ │
└─────┬─────┘ └─────┬─────┘ └──────┬─────┘
│ │ │
│ visit alice.bit.cc │ │
├──────────────────────────►│ │
│ │ CNAME point to │
│ ├───────────────────────────►│
│ │ │
│ │◄───────────────────────────┤
│ │ looking up the TXT record │
│ ├───────────────────────────►│
│ │ ├───────────┐
│ │ │ │
│ │ │ get the│ipfs content
│ │ │ │
│ return ipfs content │ │◄──────────┘
│◄──────────────────────────┼────────────────────────────┤
│ │ │
│ │ │
┌─────┴─────┐ ┌─────┴─────┐ ┌──────┴─────┐
│ │ │ │ │ │
│ Alice │ │ DNS │ │ipfs gateway│
│ │ │ │ │ │
└───────────┘ └───────────┘ └────────────┘
```

## Install
```
# run das-database and keep it synchronized with the latest data
https://github.com/DeAccountSystems/das-database
# get the code
git clone https://github.com/paicha/daslink.git
# get your Cloudflare api tokens
https://dash.cloudflare.com/profile/api-tokens
# edit config.yaml
cd config
cp config.yaml.sample config.yaml
vi config.yaml
# compile and run
go build
./daslink
```
4 changes: 2 additions & 2 deletions config/config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ db:
cloudflare:
api_key: ""
api_email: ""
zone_name: "bit.host"
zone_name: "bit.cc"
ipfs:
gateway: "cloudflare-ipfs.com"
hostname:
suffix: ".host"
suffix: ".cc"
10 changes: 3 additions & 7 deletions dao/dao_records_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dao

import (
"strings"
"time"
)

Expand All @@ -27,19 +26,16 @@ func (t *TableRecordsInfo) TableName() string {
}

func (d *DbDao) FindRecordInfoByKeys(keys []string) (recordInfo []TableRecordsInfo, err error) {
for i := 0; i < len(keys); i++ {
keys[i] = "`key` = " + "'" + keys[i] + "'"
}
err = d.db.Where(strings.Join(keys, " OR ")).Find(&recordInfo).Error
err = d.db.Where("`key` IN (?)", keys).Find(&recordInfo).Error
return
}

func (d *DbDao) FindIpfsRecordInfoByMaxId(id uint64) (recordInfo []TableRecordsInfo, err error) {
err = d.db.Where("(`key` = 'ipfs' OR `key` = 'ipns') AND id > ? ", id).Find(&recordInfo).Error
err = d.db.Where("`key` IN(?) AND id > ? ", []string{"ipfs", "ipns"}, id).Find(&recordInfo).Error
return
}

func (d *DbDao) FindIpfsRecordInfoByAccount(account string) (recordInfo []TableRecordsInfo, err error) {
err = d.db.Where("(`key` = 'ipfs' OR `key` = 'ipns') AND account = ? ", account).Find(&recordInfo).Error
err = d.db.Where("`key` IN(?) AND account = ? ", []string{"ipfs", "ipns"}, account).Find(&recordInfo).Error
return
}
30 changes: 17 additions & 13 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,27 @@ func (d *DNSData) deleteAllInvalidDNSRecord(validAccounts []string) {
}

func (d *DNSData) deleteDNSRecord(record cloudflare.DNSRecord) {
d.api.DeleteDNSRecord(ctxServer, d.zoneID, record.ID)
if record.Type == "CNAME" {
for i, cnameRecord := range *d.cnameRecords {
if cnameRecord.ID == record.ID {
*d.cnameRecords = append((*d.cnameRecords)[:i], (*d.cnameRecords)[i+1:]...)
break
err := d.api.DeleteDNSRecord(ctxServer, d.zoneID, record.ID)
if err != nil {
log.Fatalf("cloudflare DeleteDNSRecord err:%s", err)
} else {
if record.Type == "CNAME" {
for i, cnameRecord := range *d.cnameRecords {
if cnameRecord.ID == record.ID {
*d.cnameRecords = append((*d.cnameRecords)[:i], (*d.cnameRecords)[i+1:]...)
break
}
}
}
} else if record.Type == "TXT" {
for i, txtRecord := range *d.txtRecords {
if txtRecord.ID == record.ID {
*d.txtRecords = append((*d.txtRecords)[:i], (*d.txtRecords)[i+1:]...)
break
} else if record.Type == "TXT" {
for i, txtRecord := range *d.txtRecords {
if txtRecord.ID == record.ID {
*d.txtRecords = append((*d.txtRecords)[:i], (*d.txtRecords)[i+1:]...)
break
}
}
}
log.Debugf("Successfully delete %s record: %s", record.Type, record.Name)
}
log.Debugf("Successfully delete %s record: %s", record.Type, record.Name)
}

func (d *DNSData) deleteDNSRecordByAccount(account string) {
Expand Down
12 changes: 3 additions & 9 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ import (
"daslink/dao"
)

var skipIpfsRecordIndex = []int{}
var skipIpfsRecordIndex = make(map[int]bool)

func runSyncIpfsRecords(ipfsRecordList []dao.TableRecordsInfo, dnsData *DNSData, jobsChan chan string) {
// batch update dns record
validAccounts := []string{}
for index, ipfsRecord := range ipfsRecordList {
// skip the same records that have already been processed
skip := false
for _, sIndex := range skipIpfsRecordIndex {
if index == sIndex {
skip = true
break
}
}
_, skip := skipIpfsRecordIndex[index]
if skip {
continue
}
Expand Down Expand Up @@ -47,7 +41,7 @@ func findPriorityRecord(ipfsRecord dao.TableRecordsInfo, ipfsRecordList []dao.Ta
if candidateRecord.Account == priorityRecord.Account {
count += 1
if count > 1 {
skipIpfsRecordIndex = append(skipIpfsRecordIndex, index)
skipIpfsRecordIndex[index] = true
if candidateRecord.Key == priorityRecord.Key {
// select the first record if the key is the same
if priorityRecord.Id > candidateRecord.Id {
Expand Down
23 changes: 4 additions & 19 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

var pollingAccountList = &[]string{}
var pollingAccounts = sync.Map{}

func runWorker(wg *sync.WaitGroup, db *dao.DbDao, dnsData *DNSData, jobsChan chan string) {
pollingChan := make(chan string, len(jobsChan)*2)
Expand All @@ -21,7 +21,7 @@ func runWorker(wg *sync.WaitGroup, db *dao.DbDao, dnsData *DNSData, jobsChan cha
ipfsRecordList, _ := db.FindIpfsRecordInfoByAccount(account)
if len(ipfsRecordList) == 0 {
dnsData.deleteDNSRecordByAccount(account)
removeFromPollingList(account)
pollingAccounts.Delete(account)
} else {
priorityRecord := findPriorityRecord(ipfsRecordList[0], ipfsRecordList)
ipfsRecord, err := dnsData.updateDNSRecord(priorityRecord)
Expand All @@ -34,16 +34,10 @@ func runWorker(wg *sync.WaitGroup, db *dao.DbDao, dnsData *DNSData, jobsChan cha
}()
}
case account := <-jobsChan:
polling := false
for _, a := range *pollingAccountList {
if account == a {
polling = true
break
}
}
_, polling := pollingAccounts.Load(account)
if !polling {
pollingChan <- account
*pollingAccountList = append(*pollingAccountList, account)
pollingAccounts.Store(account, true)
}
case <-ctxServer.Done():
log.Info("worker exit")
Expand All @@ -54,12 +48,3 @@ func runWorker(wg *sync.WaitGroup, db *dao.DbDao, dnsData *DNSData, jobsChan cha
}()
}
}

func removeFromPollingList(account string) {
for i, a := range *pollingAccountList {
if a == account {
*pollingAccountList = append((*pollingAccountList)[:i], (*pollingAccountList)[i+1:]...)
break
}
}
}

0 comments on commit e029571

Please sign in to comment.