Hello, cindychan6688, and welcome to the Grammar Exchange.
The subject of infinitive and participle clauses is usually the subject of the main clause. Therefore, in your sentence:
Having gone through ups and downs, John and Tom are now good friends.
the subject of "having gone through ups and downs" is "John and Tom."