WebSocket Secure
This commit is contained in:
@@ -10,7 +10,7 @@ import { ProfileSettings } from "@/types/settings";
|
|||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const token = req.cookies.get("token")?.value;
|
const token = req.cookies.get("token")?.value;
|
||||||
const { username, display_name, avatar_url } =
|
const { username, display_name, avatar_url, node_address } =
|
||||||
(await req.json()) as ProfileSettings;
|
(await req.json()) as ProfileSettings;
|
||||||
|
|
||||||
if (!token)
|
if (!token)
|
||||||
@@ -21,7 +21,10 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
if (!username || !display_name || !avatar_url)
|
if (!username || !display_name || !avatar_url)
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ message: "Invalid body, requires username, display_name, avatar_url" },
|
{
|
||||||
|
message:
|
||||||
|
"Invalid body, requires username, display_name, avatar_url, node_address",
|
||||||
|
},
|
||||||
{ status: StatusCodes.BAD_REQUEST }
|
{ status: StatusCodes.BAD_REQUEST }
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -41,6 +44,7 @@ export async function POST(req: NextRequest) {
|
|||||||
username,
|
username,
|
||||||
display_name,
|
display_name,
|
||||||
avatar_url,
|
avatar_url,
|
||||||
|
node_address,
|
||||||
})
|
})
|
||||||
.eq("id", user.id);
|
.eq("id", user.id);
|
||||||
|
|
||||||
@@ -85,7 +89,7 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
const { data: profile } = await supabase
|
const { data: profile } = await supabase
|
||||||
.from("profiles")
|
.from("profiles")
|
||||||
.select("id, username, display_name, avatar_url")
|
.select("id, username, display_name, avatar_url, node_address")
|
||||||
.eq("id", user_id)
|
.eq("id", user_id)
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import MessageBox from "@/components/MessageBox";
|
import MessageBox from "@/components/MessageBox";
|
||||||
import AppSidebar from "@/components/Sidebar";
|
import AppSidebar from "@/components/Sidebar";
|
||||||
import { Message } from "@/types/types";
|
import { Message } from "@/types/types";
|
||||||
import useUser, { User } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import { NumberMap, StringMap } from "@/types/typeUtils";
|
import { StringMap } from "@/types/typeUtils";
|
||||||
import auth, { makeAddress } from "@/lib/auth";
|
|
||||||
import { useParams } from "next/navigation";
|
import { useParams } from "next/navigation";
|
||||||
|
|
||||||
// In this testing instance we are assuming that the target's node is localhost
|
// In this testing instance we are assuming that the target's node is localhost
|
||||||
@@ -16,8 +15,8 @@ export default function DMs() {
|
|||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const [messages, setMessages] = useState<Message[]>([]);
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<UserProfile>>({});
|
||||||
const [user, setUser] = useState<User | undefined>();
|
const [user, setUser] = useState<UserProfile | undefined>();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppSidebar
|
<AppSidebar
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
import { useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import AppSidebar from "@/components/Sidebar";
|
import AppSidebar from "@/components/Sidebar";
|
||||||
import { User } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import { StringMap } from "@/types/typeUtils";
|
import { StringMap } from "@/types/typeUtils";
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import ProfilePicture from "@/components/ProfilePicture";
|
import ProfilePicture from "@/components/ProfilePicture";
|
||||||
|
|
||||||
export default function Chat() {
|
export default function Chat() {
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<UserProfile>>({});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppSidebar
|
<AppSidebar
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { useParams } from "next/navigation";
|
|||||||
import MessageBox from "@/components/MessageBox";
|
import MessageBox from "@/components/MessageBox";
|
||||||
import AppSidebar from "@/components/Sidebar";
|
import AppSidebar from "@/components/Sidebar";
|
||||||
import { Message, Server as ServerType } from "@/types/types";
|
import { Message, Server as ServerType } from "@/types/types";
|
||||||
import { User } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import { StringMap } from "@/types/typeUtils";
|
import { StringMap } from "@/types/typeUtils";
|
||||||
import auth, { makeAddress } from "@/lib/auth";
|
import auth, { makeAddress } from "@/lib/auth";
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ export default function Server() {
|
|||||||
const wsNodeRef = useRef<WebSocket | null>(null);
|
const wsNodeRef = useRef<WebSocket | null>(null);
|
||||||
const [server, setServer] = useState<undefined | ServerType>();
|
const [server, setServer] = useState<undefined | ServerType>();
|
||||||
const [messages, setMessages] = useState<Message[]>([]);
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<UserProfile>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ip) return;
|
if (!ip) return;
|
||||||
|
|||||||
@@ -16,9 +16,8 @@ import { format, isToday, isYesterday } from "date-fns";
|
|||||||
import ReactMarkdown from "react-markdown";
|
import ReactMarkdown from "react-markdown";
|
||||||
import remarkGfm from "remark-gfm";
|
import remarkGfm from "remark-gfm";
|
||||||
import { NumberMap, StringMap } from "@/types/typeUtils";
|
import { NumberMap, StringMap } from "@/types/typeUtils";
|
||||||
import { User } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
|
|
||||||
import ProfilePicture from "./ProfilePicture";
|
import ProfilePicture from "./ProfilePicture";
|
||||||
|
|
||||||
function MessageContainer({
|
function MessageContainer({
|
||||||
@@ -27,14 +26,17 @@ function MessageContainer({
|
|||||||
setUserList,
|
setUserList,
|
||||||
}: {
|
}: {
|
||||||
message: Message;
|
message: Message;
|
||||||
userList: NumberMap<User>;
|
userList: StringMap<UserProfile>;
|
||||||
setUserList: React.Dispatch<React.SetStateAction<NumberMap<User>>>;
|
setUserList: React.Dispatch<React.SetStateAction<NumberMap<UserProfile>>>;
|
||||||
}) {
|
}) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
axios
|
axios
|
||||||
.get("/api/profile/", { params: { id: message.from } })
|
.get("/api/profile/", { params: { id: message.from } })
|
||||||
.then((res) =>
|
.then((res) =>
|
||||||
setUserList((prev) => ({ ...prev, [message.from]: res.data as User }))
|
setUserList((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[message.from]: res.data as UserProfile,
|
||||||
|
}))
|
||||||
)
|
)
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
}, [message.from, setUserList]);
|
}, [message.from, setUserList]);
|
||||||
@@ -144,8 +146,8 @@ export default function MessageBox({
|
|||||||
channelName,
|
channelName,
|
||||||
}: {
|
}: {
|
||||||
messages: Message[];
|
messages: Message[];
|
||||||
userList: NumberMap<User>;
|
userList: NumberMap<UserProfile>;
|
||||||
setUserList: React.Dispatch<React.SetStateAction<NumberMap<User>>>;
|
setUserList: React.Dispatch<React.SetStateAction<NumberMap<UserProfile>>>;
|
||||||
sendMessage: (m: string) => void;
|
sendMessage: (m: string) => void;
|
||||||
channelName?: string;
|
channelName?: string;
|
||||||
}) {
|
}) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import { Card } from "./ui/card";
|
|||||||
import { ScrollArea } from "./ui/scroll-area";
|
import { ScrollArea } from "./ui/scroll-area";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import useUser, { User } from "@/hooks/get-user";
|
import useUser, { UserProfile } from "@/hooks/get-user";
|
||||||
import ProfilePicture from "./ProfilePicture";
|
import ProfilePicture from "./ProfilePicture";
|
||||||
import auth, { makeAddress } from "@/lib/auth";
|
import auth, { makeAddress } from "@/lib/auth";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
@@ -47,19 +47,18 @@ export default function AppSidebar({
|
|||||||
wsRef: React.RefObject<WebSocket | null>;
|
wsRef: React.RefObject<WebSocket | null>;
|
||||||
setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
|
setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
|
||||||
onNewMessage?: (msg: Message) => void;
|
onNewMessage?: (msg: Message) => void;
|
||||||
userList: StringMap<User>;
|
userList: StringMap<UserProfile>;
|
||||||
setUserList: React.Dispatch<React.SetStateAction<StringMap<User>>>;
|
setUserList: React.Dispatch<React.SetStateAction<StringMap<UserProfile>>>;
|
||||||
chatWith?: string;
|
chatWith?: string;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
setUser?: React.Dispatch<React.SetStateAction<User | undefined>>;
|
setUser?: React.Dispatch<React.SetStateAction<UserProfile | undefined>>;
|
||||||
server?: Server | undefined;
|
server?: Server | undefined;
|
||||||
}>) {
|
}>) {
|
||||||
const [servers, setServers] = useState<[string, string][]>([]);
|
const [servers, setServers] = useState<[string, string][]>([]);
|
||||||
const [newServer, setNewServer] = useState({ name: "", ip: "" });
|
const [newServer, setNewServer] = useState({ name: "", ip: "" });
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const user = useUser();
|
const user = useUser();
|
||||||
const [userList, setUserList] = useState<StringMap<User>>({});
|
const [userList, setUserList] = useState<StringMap<UserProfile>>({});
|
||||||
const ip = "192.168.100.3";
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const s = Cookies.get("servers")?.split(",");
|
const s = Cookies.get("servers")?.split(",");
|
||||||
@@ -67,10 +66,10 @@ export default function AppSidebar({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ip || !user) return;
|
if (!user?.node_address) return;
|
||||||
if (setUser) setUser(user);
|
if (setUser) setUser(user);
|
||||||
auth(
|
auth(
|
||||||
makeAddress(ip, 7090),
|
makeAddress(user?.node_address, 7090),
|
||||||
wsRef,
|
wsRef,
|
||||||
() => {},
|
() => {},
|
||||||
setMessages,
|
setMessages,
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { ProfileSettings } from "@/types/settings";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
export interface User {
|
export interface UserProfile extends ProfileSettings {
|
||||||
id: string;
|
id: string;
|
||||||
username: string;
|
|
||||||
display_name: string;
|
|
||||||
avatar_url: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useUser() {
|
export default function useUser() {
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<UserProfile | null>(null);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -25,7 +23,7 @@ export default function useUser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = (await axios.get("/api/profile")).data as User;
|
const res = (await axios.get("/api/profile")).data as UserProfile;
|
||||||
|
|
||||||
setUser(res);
|
setUser(res);
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
+4
-2
@@ -3,7 +3,9 @@ import axios from "axios";
|
|||||||
import { Dispatch, RefObject, SetStateAction } from "react";
|
import { Dispatch, RefObject, SetStateAction } from "react";
|
||||||
|
|
||||||
export function makeAddress(ip: string, defaultPort = 7080) {
|
export function makeAddress(ip: string, defaultPort = 7080) {
|
||||||
return ip.includes(":") ? `${ip}` : `${ip}:${defaultPort}`;
|
// return ip.includes(":") ? `${ip}` : `${ip}:${defaultPort}`;
|
||||||
|
// Apparently wss doesn't like ports (or maybe the tunnel I'm using)
|
||||||
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function auth(
|
export default async function auth(
|
||||||
@@ -19,7 +21,7 @@ export default async function auth(
|
|||||||
).token;
|
).token;
|
||||||
|
|
||||||
// Open the WebSocket connection
|
// Open the WebSocket connection
|
||||||
const ws = new WebSocket(`ws://${ip}`);
|
const ws = new WebSocket(`wss://${ip}`);
|
||||||
wsRef.current = ws;
|
wsRef.current = ws;
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user