new
This commit is contained in:
81
main.go
81
main.go
@@ -7,12 +7,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p"
|
"github.com/libp2p/go-libp2p"
|
||||||
dht "github.com/libp2p/go-libp2p-kad-dht"
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
||||||
// "github.com/libp2p/go-libp2p/core/host" <-- BU SATIR SİLİNDİ (Hata sebebiydi)
|
|
||||||
"github.com/libp2p/go-libp2p/core/network"
|
"github.com/libp2p/go-libp2p/core/network"
|
||||||
"github.com/libp2p/go-libp2p/core/peer"
|
"github.com/libp2p/go-libp2p/core/peer"
|
||||||
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
|
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
|
||||||
@@ -27,32 +27,34 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// 1. Libp2p Host'u oluştur (NAT Traversal ve Relay özellikleri açık)
|
// 1. Libp2p Host'u oluştur (ZORLANMIŞ GİZLİLİK MODU)
|
||||||
h, err := libp2p.New(
|
h, err := libp2p.New(
|
||||||
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic-v1"), // TCP ve QUIC dinle
|
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic-v1"),
|
||||||
libp2p.EnableHolePunching(), // NAT arkasından geçmeyi dene
|
libp2p.EnableHolePunching(),
|
||||||
libp2p.EnableNATService(),
|
libp2p.EnableNATService(),
|
||||||
libp2p.EnableRelay(), // Relay üzerinden geçmeye izin ver
|
libp2p.EnableRelay(),
|
||||||
|
libp2p.EnableAutoRelay(),
|
||||||
|
// ÖNEMLİ: Kendimizi "Gizli" olarak işaretliyoruz ki sistem bizi Relay kullanmaya zorlasın.
|
||||||
|
libp2p.ForceReachabilityPrivate(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("[*] Düğüm Başladı ID: %s\n", h.ID())
|
fmt.Printf("[*] Düğüm Başladı ID: %s\n", h.ID())
|
||||||
fmt.Println("[*] Küresel ağa bağlanılıyor (Bootstrapping)... Bu biraz sürebilir.")
|
fmt.Println("[*] Küresel ağa bağlanılıyor (Bootstrapping)...")
|
||||||
|
|
||||||
// 2. DHT (Distributed Hash Table) Başlat
|
// 2. DHT Başlat (ModeClient: Biz sunucu değiliz, sadece istemciyiz diyoruz, daha hızlı bağlanır)
|
||||||
kademliaDHT, err := dht.New(ctx, h)
|
kademliaDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeClient))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Bootstrap (Başlangıç) Düğümlerine Bağlan
|
|
||||||
if err = kademliaDHT.Bootstrap(ctx); err != nil {
|
if err = kademliaDHT.Bootstrap(ctx); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Halka açık varsayılan IPFS düğümlerine bağlanarak ağa dahil ol
|
// 3. Bootstrap Düğümlerine Bağlan
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, peerAddr := range dht.DefaultBootstrapPeers {
|
for _, peerAddr := range dht.DefaultBootstrapPeers {
|
||||||
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
|
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
|
||||||
@@ -60,22 +62,41 @@ func main() {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if err := h.Connect(ctx, *peerinfo); err != nil {
|
if err := h.Connect(ctx, *peerinfo); err != nil {
|
||||||
// Bazılarına bağlanamamak normaldir
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("[+] Küresel ağa bağlandık! Şimdi arkadaşın aranıyor...")
|
fmt.Println("[+] IPFS Ağına Bağlanıldı.")
|
||||||
|
|
||||||
// 4. Stream Handler (Gelen mesajları karşıla)
|
// 4. RELAY ADRESİ BEKLEME DÖNGÜSÜ (YENİ)
|
||||||
|
// Relay adresi almadan reklam yaparsak kimse bize bağlanamaz.
|
||||||
|
fmt.Print("[*] Bir Relay sunucusundan yer ayrılması bekleniyor...")
|
||||||
|
for {
|
||||||
|
hasRelay := false
|
||||||
|
for _, addr := range h.Addrs() {
|
||||||
|
// Adresler içinde /p2p-circuit/ var mı diye bakıyoruz
|
||||||
|
if strings.Contains(addr.String(), "p2p-circuit") {
|
||||||
|
hasRelay = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hasRelay {
|
||||||
|
fmt.Println("\n[+] Relay Tüneli Hazır! Artık erişilebilirsin.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fmt.Print(".")
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Stream Handler
|
||||||
h.SetStreamHandler(protocolID, handleStream)
|
h.SetStreamHandler(protocolID, handleStream)
|
||||||
|
|
||||||
// 5. Discovery (Keşif) Mekanizması
|
// 6. Discovery (Keşif) - ARTIK GÜVENLE YAYIN YAPABİLİRİZ
|
||||||
routingDiscovery := routing.NewRoutingDiscovery(kademliaDHT)
|
routingDiscovery := routing.NewRoutingDiscovery(kademliaDHT)
|
||||||
dutil.Advertise(ctx, routingDiscovery, *rendezvousString)
|
dutil.Advertise(ctx, routingDiscovery, *rendezvousString)
|
||||||
fmt.Printf("[*] '%s' frekansında yayın yapılıyor.\n", *rendezvousString)
|
fmt.Printf("[*] '%s' frekansında yayın yapılıyor.\n", *rendezvousString)
|
||||||
|
|
||||||
// 6. Arkadaşı Ara ve Bağlan
|
// 7. Arkadaşı Ara ve Bağlan
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
peerChan, err := routingDiscovery.FindPeers(ctx, *rendezvousString)
|
peerChan, err := routingDiscovery.FindPeers(ctx, *rendezvousString)
|
||||||
@@ -85,29 +106,23 @@ func main() {
|
|||||||
|
|
||||||
for peer := range peerChan {
|
for peer := range peerChan {
|
||||||
if peer.ID == h.ID() {
|
if peer.ID == h.ID() {
|
||||||
continue // Kendimize bağlanmayalım
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("[?] Bulunan Peer: %s. Bağlanılıyor...\n", peer.ID)
|
fmt.Printf("[?] Bulunan Peer: %s. Bağlanılıyor...\n", peer.ID)
|
||||||
|
|
||||||
// BAĞLANTIYI DENE VE HATAYI YAZDIR
|
// Bağlantıyı daha agresif dene
|
||||||
if err := h.Connect(ctx, peer); err != nil {
|
ctxConnect, cancel := context.WithTimeout(ctx, time.Second*30)
|
||||||
// Kırmızı renkle hatayı göster
|
err := h.Connect(ctxConnect, peer)
|
||||||
fmt.Printf("\x1b[31m[!] HATA: Bağlantı başarısız: %s\x1b[0m\n", err)
|
cancel()
|
||||||
|
|
||||||
// Eğer adres listesi boşsa, sorun DHT'dedir
|
if err != nil {
|
||||||
if len(peer.Addrs) == 0 {
|
fmt.Printf("\x1b[31m[!] HATA: %s\x1b[0m\n", err)
|
||||||
fmt.Println(" -> Sebep: Peer adresi bulunamadı (Adres listesi boş).")
|
continue
|
||||||
} else {
|
|
||||||
fmt.Println(" -> Denenen adresler:", peer.Addrs)
|
|
||||||
}
|
|
||||||
|
|
||||||
continue // Sonraki denemeye geç
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("[+] BAĞLANTI BAŞARILI! Tünel Kuruldu: %s\n", peer.ID)
|
fmt.Printf("[+] BAĞLANTI BAŞARILI! Tünel Kuruldu: %s\n", peer.ID)
|
||||||
|
|
||||||
// Stream aç
|
|
||||||
stream, err := h.NewStream(ctx, peer.ID, protocolID)
|
stream, err := h.NewStream(ctx, peer.ID, protocolID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Stream hatası:", err)
|
fmt.Println("Stream hatası:", err)
|
||||||
@@ -117,13 +132,13 @@ func main() {
|
|||||||
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
|
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
|
||||||
go readData(rw)
|
go readData(rw)
|
||||||
go writeData(rw)
|
go writeData(rw)
|
||||||
return // Bir kişi bulduk ve bağlandık, aramayı durdur
|
return
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * 2) // Kimse yoksa 2 saniye sonra tekrar ara
|
time.Sleep(time.Second * 3)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {} // Programı açık tut
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStream(s network.Stream) {
|
func handleStream(s network.Stream) {
|
||||||
|
|||||||
Reference in New Issue
Block a user