aboutsummaryrefslogtreecommitdiff
path: root/webfinger/webfinger.go
blob: bcbe16ab9de58970208866f778f169f5218f39f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package webfinger

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"regexp"
	"strings"
	"time"

	"github.com/FChannel0/FChannel-Server/activitypub"
	"github.com/FChannel0/FChannel-Server/config"
	"github.com/FChannel0/FChannel-Server/util"
)

type Webfinger struct {
	Subject string          `json:"subject,omitempty"`
	Links   []WebfingerLink `json:"links,omitempty"`
}

type WebfingerLink struct {
	Rel  string `json:"rel,omitempty"`
	Type string `json:"type,omitempty"`
	Href string `json:"href,omitempty"`
}

var ActorCache = make(map[string]activitypub.Actor)

func GetActor(id string) (activitypub.Actor, error) {
	var respActor activitypub.Actor

	if id == "" {
		return respActor, nil
	}

	actor, instance := activitypub.GetActorInstance(id)

	if ActorCache[actor+"@"+instance].Id != "" {
		respActor = ActorCache[actor+"@"+instance]
		return respActor, nil
	}

	req, err := http.NewRequest("GET", strings.TrimSpace(id), nil)
	if err != nil {
		return respActor, err
	}

	req.Header.Set("Accept", config.ActivityStreams)

	resp, err := util.RouteProxy(req)

	if err != nil {
		return respActor, err
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	if err := json.Unmarshal(body, &respActor); err != nil {
		return respActor, err
	}

	ActorCache[actor+"@"+instance] = respActor

	return respActor, nil
}

//looks for actor with pattern of board@instance
func FingerActor(path string) (activitypub.Actor, error) {
	var nActor activitypub.Actor

	actor, instance := activitypub.GetActorInstance(path)

	if actor == "" && instance == "" {
		return nActor, nil
	}

	if ActorCache[actor+"@"+instance].Id != "" {
		nActor = ActorCache[actor+"@"+instance]
	} else {
		r, _ := FingerRequest(actor, instance)

		if r != nil && r.StatusCode == 200 {
			defer r.Body.Close()

			body, _ := ioutil.ReadAll(r.Body)

			json.Unmarshal(body, &nActor)
			// if err := json.Unmarshal(body, &nActor); err != nil {
			//	return nActor, err
			// }

			ActorCache[actor+"@"+instance] = nActor
		}
	}

	return nActor, nil
}

func FingerRequest(actor string, instance string) (*http.Response, error) {
	acct := "acct:" + actor + "@" + instance

	// TODO: respect https
	req, _ := http.NewRequest("GET", "http://"+instance+"/.well-known/webfinger?resource="+acct, nil)
	// if err != nil {
	//	return nil, err
	// }

	resp, err := util.RouteProxy(req)
	if err != nil {
		return resp, nil
	}

	var finger Webfinger

	if resp.StatusCode == 200 {
		defer resp.Body.Close()

		body, _ := ioutil.ReadAll(resp.Body)

		json.Unmarshal(body, &finger)
		// if err := json.Unmarshal(body, &finger); err != nil {
		//	return resp, err
		// }
	}

	if len(finger.Links) > 0 {
		for _, e := range finger.Links {
			if e.Type == "application/activity+json" {
				req, _ := http.NewRequest("GET", e.Href, nil)
				// if err != nil {
				//	return resp, err
				// }

				req.Header.Set("Accept", config.ActivityStreams)

				resp, _ := util.RouteProxy(req)
				return resp, nil
			}
		}
	}

	return resp, nil
}

func CheckValidActivity(id string) (activitypub.Collection, bool, error) {
	var respCollection activitypub.Collection

	re := regexp.MustCompile(`.+\.onion(.+)?`)
	if re.MatchString(id) {
		id = strings.Replace(id, "https", "http", 1)
	}

	req, err := http.NewRequest("GET", id, nil)
	if err != nil {
		return respCollection, false, err
	}

	req.Header.Set("Accept", config.ActivityStreams)

	resp, err := util.RouteProxy(req)
	if err != nil {
		return respCollection, false, err
	}
	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	if err := json.Unmarshal(body, &respCollection); err != nil {
		return respCollection, false, err
	}

	if respCollection.AtContext.Context == "https://www.w3.org/ns/activitystreams" && respCollection.OrderedItems[0].Id != "" {
		return respCollection, true, nil
	}

	return respCollection, false, nil
}

func CreateActivity(activityType string, obj activitypub.ObjectBase) (activitypub.Activity, error) {
	var newActivity activitypub.Activity

	actor, err := FingerActor(obj.Actor)
	if err != nil {
		return newActivity, err
	}

	newActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams"
	newActivity.Type = activityType
	newActivity.Published = obj.Published
	newActivity.Actor = &actor
	newActivity.Object = &obj

	for _, e := range obj.To {
		if obj.Actor != e {
			newActivity.To = append(newActivity.To, e)
		}
	}

	for _, e := range obj.Cc {
		if obj.Actor != e {
			newActivity.Cc = append(newActivity.Cc, e)
		}
	}

	return newActivity, nil
}

func AddFollowersToActivity(activity activitypub.Activity) (activitypub.Activity, error) {
	activity.To = append(activity.To, activity.Actor.Id)

	for _, e := range activity.To {
		aFollowers, err := GetActorCollection(e + "/followers")
		if err != nil {
			return activity, err
		}

		for _, k := range aFollowers.Items {
			activity.To = append(activity.To, k.Id)
		}
	}

	var nActivity activitypub.Activity

	for _, e := range activity.To {
		var alreadyTo = false
		for _, k := range nActivity.To {
			if e == k || e == activity.Actor.Id {
				alreadyTo = true
			}
		}

		if !alreadyTo {
			nActivity.To = append(nActivity.To, e)
		}
	}

	activity.To = nActivity.To

	return activity, nil
}

func IsValidActor(id string) (activitypub.Actor, bool, error) {
	actor, err := FingerActor(id)
	return actor, actor.Id != "", err
}

func AddInstanceToIndexDB(actor string) error {
	// TODO: completely disabling this until it is actually reasonable to turn it on
	// only actually allow this when it more or less works, i.e. can post, make threads, manage boards, etc
	return nil

	//sleep to be sure the webserver is fully initialized
	//before making finger request
	time.Sleep(15 * time.Second)

	nActor, err := FingerActor(actor)
	if err != nil {
		return err
	}

	if nActor.Id == "" {
		return nil
	}

	// TODO: maybe allow different indexes?

	obj := activitypub.ObjectBase{Id: "https://fchan.xyz/followers"}
	followers, err := obj.GetCollection()
	if err != nil {
		return err
	}

	var alreadyIndex = false
	for _, e := range followers.Items {
		if e.Id == nActor.Id {
			alreadyIndex = true
		}
	}

	if !alreadyIndex {
		actor := activitypub.Actor{Id: "https://fchan.xyz"}
		return actor.AddFollower(nActor.Id)
	}

	return nil
}

func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity activitypub.Activity) (bool, error) {
	actor, err := GetActor(activity.Object.Id)
	if err != nil {
		return false, err
	}

	req, err := http.NewRequest("POST", actor.Inbox, nil)
	if err != nil {
		return false, err
	}

	resp, err := util.RouteProxy(req)
	if err != nil {
		return false, err
	}
	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	var respActivity activitypub.Activity

	err = json.Unmarshal(body, &respActivity)
	return respActivity.Type == "Accept", err
}