Skip to content

Commit

Permalink
feat: add lassie user agent to default lipp2p host and http retriever…
Browse files Browse the repository at this point in the history
… requests
  • Loading branch information
kylehuntsman authored and hannahhoward committed May 25, 2023
1 parent 9292479 commit fecb692
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 36 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/.vscode

/cmd/lassie/lassie
/lassie
*.car

dist/
28 changes: 28 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
builds:
- main: ./cmd/lassie
binary: lassie
ldflags:
# Sets the version variable in the build package to the build version prefixed with a 'v'
# Sets the main.date to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser -X github.com/filecoin-project/lassie/pkg/build.version=v{{.Version}}
goos:
- linux
- windows
- darwin
goarch:
- 'amd64'
- 'arm64'
# Change to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
mod_timestamp: '{{.CommitTimestamp}}'
universal_binaries:
- replace: true
archives:
- format_overrides:
- goos: windows
format: zip
- goos: darwin
format: zip
release:
mode: keep-existing
changelog:
skip: true
22 changes: 0 additions & 22 deletions .goreleaser.yml

This file was deleted.

15 changes: 2 additions & 13 deletions cmd/lassie/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ package main
import (
"fmt"

"github.com/filecoin-project/lassie/pkg/build"
"github.com/urfave/cli/v2"
)

var version string // supplied during build with `go build -ldflags="-X main.version=v0.0.0"`

type Version struct {
Version string `json:"version"`
}

var versionCmd = &cli.Command{
Name: "version",
Before: before,
Expand All @@ -24,12 +19,6 @@ var versionCmd = &cli.Command{
}

func versionCommand(cctx *cli.Context) error {
if version == "" {
logger.Warn("executable built without a version")
logger.Warn("set version with `go build -ldflags=\"-X main.version=v0.0.0\"")
version = "[not set]"
}

fmt.Printf("lassie version %s\n", version)
fmt.Printf("lassie version %s\n", build.Version)
return nil
}
5 changes: 5 additions & 0 deletions pkg/build/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package build

import "fmt"

var UserAgent = fmt.Sprintf("lassie/%s", Version)
61 changes: 61 additions & 0 deletions pkg/build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package build

import (
_ "embed"
"encoding/json"
"fmt"
"os"

"github.com/filecoin-project/lassie/pkg/internal/revision"
)

var (
defaultVersion string = "v0.0.0"
// version is the built version.
// Set with ldflags in .goreleaser.yaml via -ldflags="-X github.com/filecoin-project/lassie/pkg/build.version=v{{.Version}}".
version string
// Version returns the current version of the Lassie application
Version string
)

func init() {
if version == "" {
// This is being ran in development, try to grab the latest known version from the version.json file
var err error
version, err = readVersionFromFile()
if err != nil {
// Use the default version
version = defaultVersion
}
}

Version = fmt.Sprintf("%s-%s", version, revision.Revision)
}

// versionJson is used to read the local version.json file
type versionJson struct {
Version string `json:"version"`
}

// readVersionFromFile reads the version from the version.json file.
// Reading this should be fine in development since the version.json file
// should be present in the project, I hope :)
func readVersionFromFile() (string, error) {
// Open file
file, err := os.Open("version.json")
if err != nil {
return "", err
}
defer file.Close()

// Decode json into struct
decoder := json.NewDecoder(file)
var vJson versionJson
err = decoder.Decode(&vJson)
if err != nil {
return "", err
}

// Read version from json
return vJson.Version, nil
}
42 changes: 42 additions & 0 deletions pkg/internal/revision/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Package revision provides the vsc revision, embedded by the compiler, as a
// global variable.
package revision

import (
"runtime/debug"
)

// Revision returns the revision embedded by the compiler during build.
// Suffixed with "-dirty" if modified.
var Revision string

func init() {
var revision string
var dirty bool

// Get the revision from the build info
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

for _, bs := range bi.Settings {
switch bs.Key {
case "vcs.revision":
revision = bs.Value
if len(bs.Value) > 7 {
revision = bs.Value[:7]
}
case "vcs.modified":
if bs.Value == "true" {
dirty = true
}
}
}

if dirty {
revision += "-dirty"
}

Revision = revision
}
17 changes: 16 additions & 1 deletion pkg/net/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package host
import (
"context"

"github.com/filecoin-project/lassie/pkg/build"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
Expand All @@ -32,7 +34,20 @@ func InitHost(ctx context.Context, opts []libp2p.Option, listenAddrs ...multiadd

// add muxers
opts = append([]libp2p.Option{libp2p.Muxer(yamuxID, yamuxTransport()), libp2p.Muxer(mplexID, mplex.DefaultTransport)}, opts...)
return libp2p.New(opts...)

host, err := libp2p.New(opts...)
if err != nil {
return nil, err
}

// Set the identify protocol user agent
idService, err := identify.NewIDService(host, identify.UserAgent(build.Version))
if err != nil {
return nil, err
}
idService.Start()

return host, nil
}

func yamuxTransport() network.Multiplexer {
Expand Down
2 changes: 2 additions & 0 deletions pkg/retriever/httpretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/benbjohnson/clock"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lassie/pkg/build"
"github.com/filecoin-project/lassie/pkg/events"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/filecoin-project/lassie/pkg/verifiedcar"
Expand Down Expand Up @@ -178,6 +179,7 @@ func makeRequest(ctx context.Context, request types.RetrievalRequest, candidate
req.Header.Add("User-Agent", DefaultUserAgent)
req.Header.Add("X-Request-Id", request.RetrievalID.String())

req.Header.Add("User-Agent", build.UserAgent)
return req, nil
}

Expand Down

0 comments on commit fecb692

Please sign in to comment.